今日学习记录:
While循环。
For循环。
While基本语法:
count = 0 While True: print(count, "-----") count +=1 if count = 100 break
For基本语法:
for i in range(0, 10, 2): print(i, "-----") for g in range(10) print(g) if g > 5: break
示例猜测年龄:
age = 30 count = 0 while true: guessage = int(input("guess age:")) if guessage > 30: print("think smaller") elif guessage == 30: print("configratulations! you got it!") break else: print("think bigger") count +=1 if count == 3: continue_confirm = input("do you want to continue?") if continue != "n": count = 0
优化:
age = 30 count = 0 while count < 3: guessage = int(input("guess age:")) if guessage > 30: print("think smaller") elif guessage == 30: print("configratulations! you got it!") break else: print("think bigger") count +=1 if count == 3: continue_confirm = input("do you want to continue?") if continue != "n": count = 0
For循环这个示例:
age = 30 for i in range(3): guessage = int(input("guess age:")) if guessage > 30: print("think smaller") elif guessage == 30: print("configratulations! you got it!") break else: print("think bigger") else: print("you have tried too many times...fuck off.")
原文地址:http://12306114.blog.51cto.com/12296114/1882447