您好,登錄后才能下訂單哦!
10、if else 流程判斷
舉例說明1:
import getpass #引用getpass這個模塊 _username = "kk" _password = "123456" username = input("username:") password = getpass.getpass("password") #getpass功能是讓密碼不直接顯示成明文 if _username == username and _password == password: print("Welcome user {name} login...".format(name = username)) #print("Welcome user %s login..." % username) else: print("error")
注:縮進功能:子代碼需要縮進
舉例說明2:
#猜年齡 age_of_oldboy = 65 guess_age = int(input("guess age:")) if guess_age == age_of_oldboy: print("Yes, you got it.") elif guess_age > age_of_oldboy: print("No, think bigger!") else: print("No,think smaller!")
11、while循環
舉例說明1:
#死循環 count = 0 while True: print ("count",count) count = count + 1
舉例說明2:
age_of_oldboy = 65 count = 0 #設置計數 while count < 3:#如果小于3次時循環執行 #if count == 3: #break guess_age = int(input("guess age:")) if guess_age == age_of_oldboy: print("Yes, you got it.") break elif guess_age > age_of_oldboy: print("No, think bigger!") else: print("No,think smaller!") count += 1 else: print("Funk off")
舉例說明3:
#循環猜年齡,結束提示是否繼續猜
age_of_oldboy = 65 count = 0 while count < 3: # if count == 3: # break guess_age = int(input("guess age:")) if guess_age == age_of_oldboy: print("Yes, you got it.") break elif guess_age > age_of_oldboy: print("No, think bigger!") else: print("No,think smaller!") count += 1 if count == 3:#猜了三次還沒有猜到詢問用戶是否要繼續猜 coutine_confim = input("do you want to keep guessing?:") if coutine_confim != "n": count = 0 #else:#這個就用不到了 # print("Funk off")
注:
continue:跳出本次循環,繼續到下次循環
break:結束整個循環
12、for循環
舉例說明1:
#for循環 for i in range(10): print("loop ", i)
答案: C:\Users\Documents\python>python learn01.py loop 0 loop 1 loop 2 loop 3 loop 4 loop 5 loop 6 loop 7 loop 8 loop 9
舉例說明2:
#循環猜年齡
age_of_oldboy = 65 for i in range(3): guess_age = int(input("guess age:")) if guess_age == age_of_oldboy: print("Yes, you got it.") break elif guess_age > age_of_oldboy: print("No, think bigger!") else: print("No,think smaller!") else: print("Funk off")
舉例說明3:
for i in range(0,10,2): print("loop ",i)
舉例說明4:
#大循環i執行1次,小循環執行10次 for i in range(10): print('-----------',i) for j in range(10): print (j) if j > 5:#小循環大于5就停止 break
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。