python if/while/fo条件判断
判断3次,猜年龄
方法一
1 # author = "zhuyouen" 2 age_of_zhuyouen = 35 3 4 count = 0 5 while count < 3: 6 guess_age = int(input("age:")) 7 if age_of_zhuyouen == guess_age: 8 print("age is ok") 9 break 10 elif age_of_zhuyouen > guess_age: 11 print("age is small") 12 else: 13 print("age in large") 14 count +=1 15 else: 16 print("you have test too many times,fuck off..")
D:\python35\python.exe D:/PycharmProjects/zhuyouen/0324/guess.py age:35 age is ok
方法二
1 # author = "zhuyouen" 2 age_of_zhuyouen = 35 3 4 for i in range(3): 5 guess_age = int(input("age:")) 6 if age_of_zhuyouen == guess_age: 7 print("age is ok") 8 break 9 elif age_of_zhuyouen > guess_age: 10 print("age is small") 11 else: 12 print("age in large") 13 else: 14 print("you have test too many times,fuck off..")
D:\python35\python.exe D:/PycharmProjects/zhuyouen/0324/guess_for.py age:23 age is small age:23 age is small age:35 age is ok
方法三
1 # author = "zhuyouen" 2 age_of_zhuyouen = 35 3 4 count = 0 5 while count < 3: 6 guess_age = int(input("age:")) 7 if age_of_zhuyouen == guess_age: 8 print("age is ok") 9 break 10 elif age_of_zhuyouen > guess_age: 11 print("age is small") 12 else: 13 print("age in large") 14 count +=1 15 if count == 3: 16 confirm_input = input("do you want input guess again..") 17 if confirm_input != "quit": 18 count = 0
D:\python35\python.exe D:/PycharmProjects/zhuyouen/0324/guess任意.py age:1 age is small age:1 age is small age:1 age is small do you want input guess again..quit
自增加10
1 # author = "zhuyouen" 2 3 count = 0 4 while True: 5 print("count:",count) 6 count +=1 7 if count == 10: 8 break 9 10 11 12 #for i in range(10): 13 # print("loop",i)
D:\python35\python.exe D:/PycharmProjects/zhuyouen/0324/while.py count: 0 count: 1 count: 2 count: 3 count: 4 count: 5 count: 6 count: 7 count: 8 count: 9
1 # author = "zhuyouen" 2 ''' 3 count = 0 4 while True: 5 print("count:",count) 6 count +=1 7 if count == 10: 8 break 9 ''' 10 11 12 for i in range(10): 13 print("loop",i)
D:\python35\python.exe D:/PycharmProjects/zhuyouen/0324/while.py loop 0 loop 1 loop 2 loop 3 loop 4 loop 5 loop 6 loop 7 loop 8 loop 9