所以我想我理解为什么代码没有超过 while 语句,但我不知道如何让它识别 guess 部分它并贯穿它。我已经在循环部分搜索了几个问题,但我实际上看不到它的执行情况。
num_answer = int(input('What should the answer be? '))
guesses = int(input('How many guesses? '))
answer = int(input('Guess a number: '))
while answer != num_answer and guesses != guesses:
answer = int(input('Guess a number: '))
if answer < num_answer:
print('The number is higher than that.')
guesses += 1
answer = int(input('Guess a number: '))
elif answer > num_answer:
print('The number is lower than that.')
guesses += 1
answer = int(input('Guess a number: '))
elif answer == num_answer:
print('You win!')
elif guesses > guesses and answer != num_answer:
print("You lose; the number was", num_answer, ".")
答案 0 :(得分:1)
您正在尝试将变量与自身进行比较。 &#34;猜测!=猜测&#34; 总是会返回假,所以你的&#34;而#34;永远不会被运行。 您可以将猜测变量分为两个单独的。
num_answer = int(input('What should the answer be? '))
guessesMax = int(input('How many guesses? '))
answer = int(input('Guess a number: '))
while answer != num_answer and guessesMax != guessesCount:
answer = int(input('Guess a number: '))
if answer < num_answer:
print('The number is higher than that.')
guessesCount += 1
answer = int(input('Guess a number: '))
elif answer > num_answer:
print('The number is lower than that.')
guessesCount += 1
answer = int(input('Guess a number: '))
elif answer == num_answer:
print('You win!')
elif guessesCount == guessesMax and answer != num_answer:
print("You lose; the number was", num_answer, ".")
还用&#34; ==&#34;更改了你的上一个elif。但它没用,因为
guessesCount == guessesMax
while将不会被执行;)
编辑: 你需要添加
guessesCount = 0
最重要的是它可以工作。 也在你的时间
answer = int(input('Guess a number: '))
执行两次,要么将它放在循环的开始,要么放在每个&#34; if,elif,else&#34;但现在两者都有;)