if else流程判斷python
getpass在pycharm中沒法使用,在命令行窗口中進入python環境可使用。oop
import getpass
username = input("username:") password = getpass.getpass("password:") print(username,password)
python中縮進錯誤:優化
爲何python中強制縮進,由於python中不須要定義結束符。省去告終束符,子代碼強制縮進讓結構變得更清晰。spa
最外層代碼必須頂格寫,否則就會報縮進錯誤。命令行
if else基礎程序舉例:debug
實例一:判斷用戶名密碼是否正確調試
_username = 'alex' _password = 'abc123' username = input("username:") password = input("password:") if _username == username and _password == password: print("Welcom user {name} login...".format(name=username)) else: print("Ivalid username or password")
實例二:猜年齡code
# 猜年齡 age_of_oldboy = 56 guess_age = int(input("guess age:")) if guess_age == age_of_oldboy: print("you got it!") elif guess_age < age_of_oldboy: print("think bigger...") else: print("think smaller...")
while循環orm
#最簡單的while循環程序舉例 count = 0 while True: print("count:",count) count = count+1 #至關於count +=1
實例一:猜年齡blog
#猜年齡,共猜3次,若是3次內猜對也會結束程序 age_of_oldboy = 56 count = 0 while True: if count == 3: break guess_age = int(input("guess age:")) if guess_age == age_of_oldboy: print("you got it!") break elif guess_age < age_of_oldboy: print("think bigger...") else: print("think smaller...") count +=1
實例二:對實例一代碼的優化
#猜年齡,共猜3次,若是3次內猜對也會結束程序 age_of_oldboy = 56 count = 0 while count < 3: guess_age = int(input("guess age:")) if guess_age == age_of_oldboy: print("you got it!") break elif guess_age < age_of_oldboy: print("think bigger...") else: print("think smaller...") count +=1
實例三:增長人性化提示,輸入3次錯誤密碼後會獲得提示:嘗試太屢次了。
#猜年齡,共猜3次,若是3次內猜對也會結束程序,嘗試3次後獲得提示:你嘗試的次數過多。 age_of_oldboy = 56 count = 0 while count < 3: guess_age = int(input("guess age:")) if guess_age == age_of_oldboy: print("you got it!") break elif guess_age < age_of_oldboy: print("think bigger...") else: print("think smaller...") count +=1 if count == 3: print("you have tried too many times...")
實例四:對實例三程序的優化。提示代碼的判斷能夠直接用else。
#猜年齡,共猜3次,若是3次內猜對也會結束程序,嘗試3次後獲得提示:你嘗試的次數過多。 age_of_oldboy = 56 count = 0 while count < 3: guess_age = int(input("guess age:")) if guess_age == age_of_oldboy: print("you got it!") break elif guess_age < age_of_oldboy: print("think bigger...") else: print("think smaller...") count +=1 else: print("you have tried too many times...")
for循環
實例一,最簡單的for循環程序
for i in range(10): print("loop",i)
等於如下:
等於如下: for i in range(0,10,1): #步長默認爲1 print("loop",i)
i,臨時變量
range,至關於定義了(0,1,2,3,4,5,6,7,8,9) 每循環一次i按順序取值一次。
實例二:上節課中的while循環實例改成for循環:
#猜年齡,共猜3次,若是3次內猜對也會結束程序,嘗試3次後獲得提示:你嘗試的次數過多。 age_of_oldboy = 56 for i in range(3): guess_age = int(input("guess age:")) if guess_age == age_of_oldboy: print("you got it!") break elif guess_age < age_of_oldboy: print("think bigger...") else: print("think smaller...") else: print("you have tried too many times...")
實例三,打印10之內的偶數:
for i in range(0,10,2): #2爲步長 print("loop",i)
實例四,優化while猜年齡程序
#猜年齡,共猜3次,嘗試3次後詢問是否繼續,若是回答:n,則結束程序;其餘則從新開始程序。 age_of_oldboy = 56 count = 0 while count < 3: guess_age = int(input("guess age:")) if guess_age == age_of_oldboy: print("you got it!") break elif guess_age < age_of_oldboy: print("think bigger...") else: print("think smaller...") count +=1 if count ==3: continue_confirm = input("do you want to keep guessing?") if continue_confirm != "n": count = 0
break和continue的區別,根據下面2段代碼,使用debug調試功能在pycharm中運行,觀察後得知
代碼一:
# continue的做用是結束本次循環,不會終止for循環 for i in range(0,10): if i <3: print("loop",i) else: continue print("hehe...")
代碼二:
# break是結束當前循環 for i in range(0,10): if i <3: print("loop",i) else: break print("hehe...")
循環嵌套
for i in range(0,10): print("--------",i) for j in range(10): print(j) if j >5: break
查看輸出:小循環輸出0-6,大循環輸出0-9,brake只中斷當前循環。