python的for,while及if

來源:互聯網
上載者:User

標籤:int   年齡   error   led   密碼認證   use   運行   for迴圈   nbsp   

python的迴圈及判斷認證賬戶和密碼
import getpass  #匯入模組username = input(‘username: ‘)password = getpass.getpass(‘password: ‘)  #輸入密碼不顯示使用者密碼認證:if及else# Author:Yao Xiaodongimport getpass_username = ‘Arthur‘_password = ‘123456‘ username = input(‘username: ‘)password = input(‘password: ‘) if _username == username and _password == password:    print(‘welcome {name} to python world‘.format(name=username))else:    print(‘auth filed‘)
猜測年齡while方式
#while迴圈authur_age = 500count = 0while count < 3:    guess_age = int(input(‘guess_age: ‘))    if guess_age == authur_age :        print(‘yes,you are right!‘)        break    elif guess_age > authur_age :        print(‘It is to bigger...‘)    else:        print(‘It is to smaller...‘)    count += 1else:    print(‘Error‘)
for方式
for迴圈authur_age = 500for i in range(3):    guess_age = int(input(‘guess_age: ‘))    if guess_age == Yxd_age :        print(‘yes,you are right!‘)        break    elif guess_age > Yxd_age :        print(‘It is to bigger...‘)    else:        print(‘It is to smaller...‘)else:    print(‘Error‘)
 while+if判斷三次後是否繼續
age = 21count = 0while count < 3:    guess_count = int(input("guess_count: "))    if guess_count == age:        print(‘yes‘)
break
elif guess_count > age: print(‘big‘) else: print(‘small‘) count += 1 if count == 3: paly = input(‘do you want paly agen?(y/n): ‘) if paly == ‘y‘: count = 0 elif paly == ‘n‘: print(‘all right‘)
else: print(‘(y/n)do you know?‘)

break和continue的有什麼不同?

不同點

break跳出整個迴圈,如果迴圈中遇到了break,那麼這個迴圈直接無效。

continue跳出當前迴圈,如果迴圈中遇到了continue,跳出本次迴圈繼續下次迴圈。

 

相同點:

break和continue都是只跳出當前迴圈,也就是說如果是for迴圈套for迴圈的時候,只會跳出break或者continue所在的那個for迴圈。舉個例子:

例如:

for i in range(3):    print(‘---------‘,i,‘-------‘)    for n in range(3):        if n > 1:            print(n)        else:            break#運行結果為--------- 0 ---------------- 1 ---------------- 2 -------
#也就是說遇到了break後整個迴圈都沒有了結果,但是外迴圈沒有影響

再看continue

for i in range(3):    print(‘---------‘,i,‘-------‘)    for n in range(3):        if n > 1:            print(n)        else:            continue#運行結果為--------- 0 -------2--------- 1 -------2--------- 2 -------2
#也就是說continue只是跳出一次迴圈,而不是整個迴圈,
但是外迴圈沒有影響

 

    

python的for,while及if

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.