python表达式if-for-while
Python 表达式
死循环and跳出
While true
lucky_num = 27 while True: ##死循环 input_num = int(input("input your guess number: ")) if input_num == lucky_num: print("bingo!") break ##跳出循环 elif input_num > lucky_num: print("the real number is smaller!") else: print("the real number is biger!")
C:\Python36\python.exe C:/Users/7KM/PycharmProjects/s18/s18/day1/namber.py
input your guess number: 12
the real number is biger!
input your guess number: 27
bingo!
进程已结束,退出代码0
非While true
luck_num=27 input_num=-1 while input_num != luck_num: input_num = int(input("please input your guess number:")) if input_num < luck_num: print ("your input smaller ,try input agein") elif input_num > luck_num: print ("your input bigger ,try input agein") print("bingo")
please input your guess number:22
your input smaller ,try input agein
please input your guess number:33
your input bigger ,try input agein
please input your guess number:27
bingo
进程已结束,退出代码0
计数器限制
luck_num = 27 input_num = -1 count = 0 while input_num != luck_num and count <3: #print ("input count:" ,count) input_num = int(input("please input your guess number:")) if input_num < luck_num: print ("your input smaller ,try input agein") elif input_num > luck_num: print ("your input bigger ,try input agein") #count = count + 1 count += 1 if luck_num == input_num: print("bingo") else: print("too many tries!")
luck_num = 27 input_num = -1 count = 0 #while input_num != luck_num and count <3: while count < 3: #print ("input count:" ,count) input_num = int(input("please input your guess number:")) if input_num < luck_num: print ("your input smaller ,try input agein") elif input_num > luck_num: print ("your input bigger ,try input agein") #count = count + 1 #count += 1 else: print("bingo") break count += 1 else: print("too many tries!")
for 循环
luck_num = 27 input_num = -1 count = 0 #while input_num != luck_num and count <3: for i in range (3); #print ("input count:" ,count) input_num = int(input("please input your guess number:")) if input_num < luck_num: print ("your input smaller ,try input agein") elif input_num > luck_num: print ("your input bigger ,try input agein") #count = count + 1 #count += 1 else: print("bingo") break count += 1 else: print("too many tries!")