下面以具体代码示例来展示while for if 使用的基本语法
1 # _*_ coding:utf-8 _*_ 2 #此程序为实现猜年龄,用户有三次机会。三次后由用户决定是否继续再次获取三次猜测机会。 3 zsq_age = 31 4 flag = 0 5 while flag == 0: 6 for i in range(0,3): #此循环为用户每次选择继续猜后有三次猜的机会,此处与16行构成类似if/else条件语法 7 age = int(input("inpute your guess age")) 8 if age == zsq_age: 9 print("you are right ...") 10 flag = 1 #猜对后设置破坏外层while循坏条件 11 break #猜对后跳出for循环,跳至while循环;上一行已经破坏了while循环,整个两层循环结束. 12 elif age > zsq_age: 13 print("your answer is bigger,please guess to small") 14 else: 15 print("your answer is smaller,please guess to large") 16 else: 17 try_flag = input("do you want keep trying?,inpute 'n' you will have three chance to guess") #用户是否需要再次获得三次猜测机会判断 18 if str.upper(try_flag) == "N": 19 print("bye bye") 20 break