1.1 if else
猜测年龄:(一次结束)
1 age_of_zhou = 27 2 3 guess_age = int(input("guess age:")) 4 5 if guess_age == age_of_zhou: 6 7 print("yes,you got it.") 8 9 elif guess_age > age_of_zhou:10 11 print("think smaller...")12 13 else:14 15 print("think bigger!")
1.2 while循环
1 count = 02 3 while True: #死循环4 5 print("count:",count)6 7 count +=1
猜测年龄(连续猜测,最多三次)
1 age_of_zhou = 27 2 3 count = 0 4 5 while True: 6 7 if count ==3: #利用count计数器,实现循环次数限制 8 9 break10 11 guess_age = int(input("guess age:"))12 13 if guess_age == age_of_zhou:14 15 print("yes,you got it.")16 17 break18 19 elif guess_age > age_of_zhou:20 21 print("think smaller...")22 23 else:24 25 print("think bigger!")26 27 count +=1
代码优化:
1 age_of_zhou = 27 2 3 count = 0 4 5 while count <3: #直接while count 6 7 guess_age = int(input("guess age:")) 8 9 if guess_age == age_of_zhou:10 11 print("yes,you got it.")12 13 break14 15 elif guess_age > age_of_zhou:16 17 print("think smaller...")18 19 else:20 21 print("think bigger!")22 23 count +=1
失败三次后,有提示
1 age_of_zhou = 27 2 3 count = 0 4 5 while count <3: 6 7 guess_age = int(input("guess age:")) 8 9 if guess_age == age_of_zhou:10 11 print("yes,you got it.")12 13 break14 15 elif guess_age > age_of_zhou:16 17 print("think smaller...")18 19 else:20 21 print("think bigger!")22 23 count +=124 25 if count ==3:26 27 print("you have tried too many times..fuck off")28 或者29 30 age_of_zhou = 2731 32 count = 033 34 while count <3:35 36 guess_age = int(input("guess age:"))37 38 if guess_age == age_of_zhou:39 40 print("yes,you got it.")41 42 break43 44 elif guess_age > age_of_zhou:45 46 print("think smaller...")47 48 else:49 50 print("think bigger!")51 52 count +=153 54 else:55 56 print("you have tried too many times..fuck off")
1.3 for循环
1 for i in range(10): #从0开始打印,不包括最后一个数2 3 print ("loop",i)
猜测年龄:
1 age_of_zhou = 27 2 3 for i in range(3): 4 5 guess_age = int(input("guess age:")) 6 7 if guess_age == age_of_zhou: 8 9 print("yes,you got it.")10 11 break12 13 elif guess_age > age_of_zhou:14 15 print("think smaller...")16 17 else:18 19 print("think bigger!")20 21 else:22 23 print("you have tried too many times..fuck off")
设置步长为2
for i in range(0,10,2): #从0开始,不包括最后一个数 print ("loop",i)
猜测年龄,提示还继续吗
1 age_of_zhou = 27 2 3 count = 0 4 5 while count <3: 6 7 guess_age = int(input("guess age:")) 8 9 if guess_age == age_of_zhou:10 11 print("yes,you got it.")12 13 break14 15 elif guess_age > age_of_zhou:16 17 print("think smaller...")18 19 else:20 21 print("think bigger!")22 23 count +=124 25 if count ==3:26 27 countinue_confirm = input("do you want to keep guessing? Y/N:")28 29 if countinue_confirm == "Y":30 31 count = 0 #count复位,重新计数 32 33 elif countinue_confirm =="N":34 35 break #break结束整个循环
1.4 countinue、break、exit()
1、continue是结束本次循环跳到下一个循环
1 for i in range(0,10):2 j = int(input("input:"))3 if i==j:4 continue
5 else: 6 print(i,j) 7 else: 8 print("current") 9 print("hello")
2、break是结束当前循环层
1 for i in range(0,10):2 j = int(input("input:"))3 if i==j:4 break5 else:6 print(i,j)7 else:8 print("current")9 print("hello")
3、exit()退出当前整个程序
1 for i in range(0,10):2 j = int(input("input:"))3 if i==j:4 exit("退出程序")5 else:6 print(i,j)7 else:8 print("current")9 print("hello")
双重循环
1 for i in range(10): 2 3 print("----------",i) 4 5 for j in range(10): 6 7 print(j) 8 9 if j >5:10 11 break