python流程式控制制

來源:互聯網
上載者:User

標籤:div   ssi   tin   ctr   exe   end   判斷   use   int   

>>> True or False and FalseTrue首先判斷前兩個所以得到True之後就不用判斷了#########################>>> (True or False) and FalseFalse###########流程式控制制if...else單分支if 條件:    :是文法格式    滿足條件後執行代碼   縮排4個空格這是判斷成立後執行的代碼AgeOfOldboy = 56if AgeOfOldboy > 50 :    print("Too old,time to retire..")加註釋快速鍵  ctrl+/雙分支age_of_oldboy=50if age_of_oldboy > 100:    print(‘too old,time to end‘)else:    print(‘impossible‘)age_of_oldboy=50age_input=int(input(‘input age:‘))if age_input > age_of_oldboy:    print(‘too big‘)elif age_input < age_of_oldboy:    print(‘too small‘)else:    print(‘you get it‘)#########練習############成績ABCDE五個等級與分數對應如下A 90-100B 80-89C 60-79D 40-59E 0-39要求使用者輸入0-100的數字,能正確的列印成績scroce = int(input("輸入分數:"))if scroce > 100:    print("我擦,最高分才100")elif scroce >= 90:    print("A")elif scroce >= 80:    print("B")elif scroce >= 70:    print("C")elif scroce >= 60:    print("D")else:    print("E")###################################迴圈從1列印到3count=1while count <= 3:    print(count)    count+=1###################################在等於10的時候跳出迴圈count=1while count <= 100:    if count == 10:        break #跳出本層迴圈    print(count)    count+=1###################################count=0while count <5:    if count == 3:        count+=1        continue    print(count)    count+=11、count=0 滿足<5   print 0   0+1=12、count=1 滿足<5   print=1    1+1=23、count=2 滿足<5   print=2   2+1=34、count=3 滿足<5   滿足if count == 3:   因為continue是跳出本次迴圈所以   count+=1可以是迴圈不在這裡卡住   3+1=45、count=4 滿足<5   print=4   因迴圈必須小於5所以停止迴圈##############列印0-10的偶數count=0while count <= 10:    if count % 2 == 0:        print(count)    count+=1#################count=0while count <= 10:    if count % 2 == 0:        count+=1        continue    print(count)    count+=1列印奇數####################列印小於3並且大於96的數(0-100)count=1while count <= 100:    if count >=3 and count <=96:        count+=1        continue    print(count)    count+=1###########################猜年齡只能猜3次age_of_oldboy=50count=1while count <=3:    age_inp=int(input(‘input age:‘))    if  age_inp > age_of_oldboy:        print(‘too big‘)        count+=1    elif age_inp < age_of_oldboy:        print(‘too small‘)        count+=1    else:        print(‘you get ot‘)        break#################################無限迴圈while True:    cmd=input(‘>>: ‘)   #int只能把123這種字串轉換為形式    if cmd == ‘q‘:        break#################################輸入賬戶密碼3次機會count=0while True:    if count > 2:        print(‘too many tries‘)        break    user=input(‘user: ‘)    password=input(‘password ‘)    if user == ‘egon‘ and password == ‘123‘:        print(‘login successful‘)        break    else:        print(‘login err‘)        count+=1count=0while count <3:    if count > 2:        print(‘too many tries‘)        break    user=input(‘user: ‘)    password=input(‘password ‘)    if user == ‘egon‘ and password == ‘123‘:        print(‘login successful‘)        break    else:        print(‘login err‘)        count+=1#############################count=0tag=Truewhile tag:    if count > 2:        print(‘too many tries‘)        break    user=input(‘user: ‘)    password=input(‘password ‘)    if user == ‘egon‘ and password == ‘123‘:        print(‘login successful‘)        while tag:            cmd=input(‘>>: ‘)            if cmd == ‘q‘:                tag=False                continue            print(‘exec %s‘ %cmd)    else:        print(‘login err‘)        count+=1#############while else  連用count=1while count <=5 :    print(count)    count+=1else: #while 沒有被break打斷的時候才執行else的子代碼    print(‘=========>‘)count=1while count <=5 :    if count ==2 :        break    print(count)    count+=1else: #while 沒有被break打斷的時候才執行else的子代碼    print(‘=========>‘)

  

python流程式控制制

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.