Python 3.x 为什么我的Python代码不离开这个while循环?
我应该写一个游戏,会问随机减法问题。当用户可以输入0继续下一个问题,或输入-1退出时。当它退出时,它将打印用户的分数,并询问他们是否想再次玩。如果用户连续得到3个错误答案,游戏应该结束。然而,我似乎可以让我的while循环工作。它达到3,然后重新设置。它似乎永远不会结束游戏并询问用户是否想再次玩 我错过了什么Python 3.x 为什么我的Python代码不离开这个while循环?,python-3.x,while-loop,Python 3.x,While Loop,我应该写一个游戏,会问随机减法问题。当用户可以输入0继续下一个问题,或输入-1退出时。当它退出时,它将打印用户的分数,并询问他们是否想再次玩。如果用户连续得到3个错误答案,游戏应该结束。然而,我似乎可以让我的while循环工作。它达到3,然后重新设置。它似乎永远不会结束游戏并询问用户是否想再次玩 我错过了什么 import random highscore = 0 playAgain = 'Y' while playAgain == 'Y': correctCount = 0 totalC
import random
highscore = 0
playAgain = 'Y'
while playAgain == 'Y':
correctCount = 0
totalCount = 0
wrongCount = 0
cont = 0
if wrongCount >=3:
playAgain = eval(input("Do you want to play again? Y for Yes and N for No: "))
while wrongCount < 3:
num1 = random.randint(0,9)
num2 = random.randint(0,9)
if num1 < num2:
num1, num2 = num2, num1
answer = eval(input("What is "+str(num1)+" - "+str(num2)+" ?\n"))
if num1 - num2 == answer:
print("You are correct!\n")
totalCount += 1
correctCount += 1
#cont = int(input("Do you want to continue? 0 for Yes and -1 for No: "))
if num1 - num2 != answer:
print("Sorry, that answer is incorrect\n")
totalCount += 1
wrongCount += 1
score = (correctCount / totalCount) * 100
print("You answered "+correctCount+" questions right, out of a total of
"+totalCount+" questions.\nYour score is: "+score)
if score > highscore:
print("You scored the new high score!")
playAgain = eval(input("Do you want to play again?"))
随机导入
高分=0
再次播放='Y'
再次播放时==“Y”:
更正计数=0
totalCount=0
错误计数=0
cont=0
如果错误计数>=3:
playreach=eval(输入(“您想再次播放吗?Y表示是,N表示否:”)
当错误计数小于3时:
num1=random.randint(0,9)
num2=random.randint(0,9)
如果num1高分:
打印(“您获得了新的高分!”)
playreach=eval(输入(“您想再次播放吗?”))
这就是你想要的一切,享受吧。。。
注意if条件下的correctCount和ErrorCount
import random
highscore = 0
playAgain = 'Y'
while playAgain == 'Y':
correctCount = 0
totalCount = 0
wrongCount = 0
cont = 0
# if wrongCount >=3:
# playAgain = int(input("Do you want to play again? Y for Yes and N for No: "))
while wrongCount < 3:
num1 = random.randint(0,9)
num2 = random.randint(0,9)
if num1 < num2:
num1, num2 = num2, num1
answer = int(input("What is " + str(num1)+ " - " + str(num2) + " ?\n"))
if num1 - num2 == answer:
print("You are correct!\n")
correctCount += 1
if num1 - num2 != answer:
print("Sorry, that answer is incorrect\n")
wrongCount += 1
totalCount += 1
#cont = int(input("Do you want to continue? 0 for Yes and -1 for No: "))
score = (correctCount / totalCount) * 100
print("You answered "+str(correctCount)+" questions right, out of a total of "+str(totalCount)+" questions.\nYour score is: "+str(score))
if score > highscore:
highscore = score
print("You scored the new high score!")
playAgain = raw_input("Do you want to play again? Y for Yes and N for No: ")
随机导入
高分=0
再次播放='Y'
再次播放时==“Y”:
更正计数=0
totalCount=0
错误计数=0
cont=0
#如果错误计数>=3:
#playreach=int(输入(“您想再次播放吗?Y表示是,N表示否:”)
当错误计数小于3时:
num1=random.randint(0,9)
num2=random.randint(0,9)
如果num1高分:
高分
打印(“您获得了新的高分!”)
playreach=raw_输入(“您想再次播放吗?Y表示是,N表示否:”)
缩进已关闭。请确保复制真实代码。在增加totalCount和correctCount的if语句下,您需要将其正确缩进,以便仅在if条件下工作,类似地,在另一个if中增加totalCount和ErrorCount时。