標籤: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