1.最简单的while True循环
1 count = 0 2 while True : 3 if count == 10: 4 print('hello',count) 5 break 6 count += 1
hello 10
2.利用while循环写一个猜年龄程序:有3次机会,猜正确直接退出
1 age=25 2 count=0 3 while count < 3: 4 guess_age=input("age>>:") 5 if guess_age.isdigit(): 6 guess_age=int(guess_age) 7 else: 8 print('请输入数字') 9 continue 10 if guess_age > 25: 11 print("往小猜") 12 elif guess_age < 25: 13 print("往大猜") 14 else: 15 print("猜对了") 16 break 17 count += 1 18 if count == 3: 19 print(' 3次机会已用完')
1 age>>:df 2 请输入数字 3 age>>:1 4 往大猜 5 age>>:67 6 往小猜 7 age>>:25 8 猜对了