1 age = 50 2 #user_input_age = int(input("Age is:")) 3 4 flag = True 5 while flag: 6 user_input_age = int(input("Age is :")) 7 if user_input_age == age: 8 print("Yes") 9 flag = False 10 elif user_input_age > age: 11 print("Is bigger") 12 else: 13 print("Is smaller") 14 print("end")
终止死循环语句1:while语句循环小节,可以将True进行变量,才可以进行变换,让True变成Flalse。
age = 50 #user_input_age = int(input("Age is:")) #flag = True while True: user_input_age = int(input("Age is :")) if user_input_age == age: print("Yes") break elif user_input_age > age: print("Is bigger") else: print("Is smaller") print("end")
终止死循环语句2:不用变量直接上,用break语句直接结算循环。
1 # continus 2 3 num = 1 4 5 while num <= 10: 6 num += 1 7 if num == 3: 8 continue 9 print(num) 10 else: 11 print("This is else statement")
单次循环终止:continus 终止循环中的一次。
num1 = 0 while num1<=5: print(num1,end="_") num2 = 0 while num2<=7: print(num2,end="-") num2+=1 num1+=1 print() #等价于 print(end="\n")
0_0-1-2-3-4-5-6-7- 1_0-1-2-3-4-5-6-7- 2_0-1-2-3-4-5-6-7- 3_0-1-2-3-4-5-6-7- 4_0-1-2-3-4-5-6-7- 5_0-1-2-3-4-5-6-7-
默认换行
制作乘法表