笔记循环控制/次数限制

用break版本循环

lucky_num = 19


while True:    #如果为真就循环
input_num = int(input("Input the guess num:"))
if input_num == lucky_num:
    print("bingo!")
    break
elif input_num > lucky_num:
    print("the real numer is smaller.")
else:
    print("the real num is bigger...")

 

不用break版本循环

lucky_num = 19
input_num = -1

guess_count = 0 

#while lucky_num != input_num and guess_count < 3:     !=为不等的意思。

while guess_count < 3:
    input_num = int(input("Input the guess num: "))
    if input_num > lucky_num:
        print("the real numer is smaller.")
    elif input_num < lucky_num:
        print("the real num is bigger..")
    else:
        print("Bingo!")
        break

    guess_count += 1

else:
    print("Too many retrys!")

#if lucky_num == input_num:
#print("Bingo!")
#else:
#print("Too many retrys!")

for..in版本循环

lucky_num = 19
input_num = -1

for i in range(3):
    input_num = int(input("请输入数字: "))
    if input_num > lucky_num:
        print("你输入的数字过大...")
    elif input_num < lucky_num:
        print("你输入的数字过小...")
    else:
        print("恭喜你,猜对了!")
        break

else:
    print("Too many retrys!")

posted @ 2017-08-27 17:47  耍猴子的大叔  阅读(168)  评论(0编辑  收藏  举报