2016-08-18 104 views
0

我正在为范围1 - 100的用户输入建立一个猜测程序。为什么我的Python代码跳过一个while循环?

为什么它会跳过第二个while循环,检查用户输入并转发它。它直接以数字1

import random 

nums_lasted = [] 
a = 0 
while a < 101: 
    nums_lasted.append(a) 
    a += 1 
secret_num = 1 
while secret_num < 0 or secret_num > 100: 
    try: 
     secret_num = int(input("My number is")) 
    except ValueError: 
     print("No way that was an integer!") 
guess_pc = 50 
min = 50 
max = 101 
while True: 
    print("Is it", guess_pc,"?") 
    if guess_pc == secret_num: 
     print("Easy") 
     break 
    elif guess_pc > secret_num: 
     max = guess_pc 
     nums_lasted.append(guess_pc) 
     nums_lasted1 = [i for i in nums_lasted if i < guess_pc] 
     nums_lasted = nums_lasted1 
    elif guess_pc < secret_num: 
     min = guess_pc 
     nums_lasted.append(guess_pc) 
     nums_lasted1 = [i for i in nums_lasted if i < guess_pc] 
     nums_lasted = nums_lasted1 
    guess_pc = random.choice(nums_lasted) 
+1

提示:'1 < 0 or 1 > 100'的真值是多少? – Kevin

+1

顺便说一句,在Python中,当'secret_num == 1'时,你可以做'虽然不是0 <= secret_number <= 100:' –

回答

1

由于条件secret_num < 0 or secret_num > 100secret_num == 1假的。 1肯定在0到100之间。你应该将secret_number设置为大于100的值,以便按预期工作。

+0

' secret_num <0或secret_num> 100'是错误的 – DeepSpace

+0

当然,谢谢@DeepSpace。 –

3
secret_num = 1 
while secret_num < 0 or secret_num > 100: 

您设置secret_num1while只会在secret_num小于0或大于100时运行,因此它将永远不会执行。

+0

我需要睡觉 – Metalnakls