Python While循环在为False时不退出
我在使用!=在我的数学游戏中。我的while循环评估为False,但它并没有像我预期的那样退出循环。它只是卡在循环中,任何输入都不会退出Python While循环在为False时不退出,python,Python,我在使用!=在我的数学游戏中。我的while循环评估为False,但它并没有像我预期的那样退出循环。它只是卡在循环中,任何输入都不会退出 import random numCorrect = 0 operand1 = 0 operand2 = 0 operator = 0 correctAnswer = 0 userInput = 0 playAgain = "Y" print("Welcome to the Looping Math Game!\nAnsw
import random
numCorrect = 0
operand1 = 0
operand2 = 0
operator = 0
correctAnswer = 0
userInput = 0
playAgain = "Y"
print("Welcome to the Looping Math Game!\nAnswer these questions, and get 5 correct to win.")
while playAgain == "Y":
while numCorrect != 1:
operand1 = random.randint(1,10)
operand2 = random.randint(1,10)
operator = random.randint(1,3)
if operator == 1:
correctAnswer = operand1 * operand2
userInput = int(input("{} * {} = ".format(operand1, operand2)))
if userInput == correctAnswer:
print("Correct!")
numCorrect +=1
else:
print("Incorrect...")
if operator == 2:
correctAnswer = operand1 + operand2
userInput = int(input("{} + {} = ".format(operand1, operand2)))
if userInput == correctAnswer:
print("Correct!")
numCorrect +=1
else:
print("Incorrect...")
if operator == 3:
correctAnswer = operand1 - operand2
userInput = int(input("{} - {} = ".format(operand1, operand2)))
if userInput == correctAnswer:
print("Correct!")
numCorrect +=1
else:
print("Incorrect...")
print("You win!")
playAgain = input("Would you like to play again? (Y/N): ")
while playAgain != "Y" or playAgain !="N":
print("Invalid input. Try again!")
playAgain = input("Would you like to play again? (Y/N): ")
您的代码实际上非常接近函数。我决定将它弹出到我的IDE中,看看需要多少修复。下面是我在做这件事时做的一些笔记:
是一个无限循环,因为其中的任何代码都不会更改而numCorrect!=1:
的值。我知道你在那里想干什么numCorrect
- 您需要第二个循环来不断提问,直到用户答对5个问题,因此我们确实需要
而
循环…只需要将其移出一个级别
- 底部的代码需要在外部循环中,这样,如果用户想再次播放,代码流可以返回到开始(更新:看起来您已经解决了这个问题)
- 再次播放时显示行
是另一个无限循环。想想看。一封信不能同时是两件事,所以它总是不是“Y”就是不是“N”。您只需要在此处使用“Y”或“再玩一次!”=“N”:
,而不是和
或
import random
numCorrect = 0
operand1 = 0
operand2 = 0
operator = 0
correctAnswer = 0
userInput = 0
playAgain = "Y"
correctToWin = 5
print("Welcome to the Looping Math Game!\nAnswer these questions, and get {} correct to win.".format(correctToWin))
while playAgain == "Y":
numCorrect = 0
while numCorrect < correctToWin:
operand1 = random.randint(1,10)
operand2 = random.randint(1,10)
operator = random.randint(1,3)
if operator == 1:
correctAnswer = operand1 * operand2
userInput = int(input("{} * {} = ".format(operand1, operand2)))
if userInput == correctAnswer:
print("Correct!")
numCorrect +=1
else:
print("Incorrect...")
if operator == 2:
correctAnswer = operand1 + operand2
userInput = int(input("{} + {} = ".format(operand1, operand2)))
if userInput == correctAnswer:
print("Correct!")
numCorrect +=1
else:
print("Incorrect...")
if operator == 3:
correctAnswer = operand1 - operand2
userInput = int(input("{} - {} = ".format(operand1, operand2)))
if userInput == correctAnswer:
print("Correct!")
numCorrect +=1
else:
print("Incorrect...")
print("You win!")
playAgain = input("Would you like to play again? (Y/N): ")
while playAgain != "Y" and playAgain !="N":
print("Invalid input. Try again!")
playAgain = input("Would you like to play again? (Y/N): ")
随机导入
numCorrect=0
操作数1=0
操作数2=0
运算符=0
正确答案=0
用户输入=0
playreach=“Y”
修正Towin=5
打印(“欢迎参加循环数学游戏!\n回答这些问题,并获得{}正确答案以获胜。”.format(correctToWin))
再次播放时==“Y”:
numCorrect=0
当numCorrect
您是否使用了返回
或中断
语句?您需要编辑代码。。缩进被打断了,循环中没有任何东西可以让它退出。没有中断,您永远不会更改循环控制变量的值。你认为它会怎么停止?你没有跟踪执行情况吗?我想我修正了缩进。它破坏了代码中的复制。我认为我的理解是不完整的,但为什么我需要一个中断声明。我的理解是,如果while计算为False,它将退出循环。我应该把中断声明放在哪里?谢谢你,非常感谢你解释了无限循环,这是我遇到的主要问题。另外,对于导致问题诊断困难的其他问题,大家也表示抱歉。