我正在爲範圍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 < 0 or 1 > 100'的真值是多少? – Kevin
順便說一句,在Python中,當'secret_num == 1'時,你可以做'雖然不是0 <= secret_number <= 100:' –