为什么这不起作用(python)
意味着给你3次尝试,然后显示答案是否正确。也意味着如果问题在第一次或第二次尝试时是正确的,则中断。您需要在while循环中获得答案,以便while循环检查用户是否给出了正确的答案 您如何使用为什么这不起作用(python),python,Python,意味着给你3次尝试,然后显示答案是否正确。也意味着如果问题在第一次或第二次尝试时是正确的,则中断。您需要在while循环中获得答案,以便while循环检查用户是否给出了正确的答案 您如何使用x也存在问题x将永远不会为0,因此将永远不会触及断线。有关这两个问题的解决方案,请参见下面的代码 score = 0 print("Question 1. What is 1+1?") print("a) 2") print("b) 4") print("c) 11") print("d) 3") x = 0
x
也存在问题x
将永远不会为0,因此将永远不会触及断线。有关这两个问题的解决方案,请参见下面的代码
score = 0
print("Question 1. What is 1+1?")
print("a) 2")
print("b) 4")
print("c) 11")
print("d) 3")
x = 0
line = input('Answer: ')
while line != "a":
x = x+1
print('Incorrect, you have used',x,"of your 3 chances")
if x == 0:
break
print("Question 2. What is 10x22?")
我希望这能回答你的问题。快乐编码 您不要求在循环中输入,因此如果输入了错误的答案,
line
将保留旧答案。此外,您增加x
,因此x
将永远不会等于0,因此中断将永远不会发生。如果答案不正确,则不工作将创建无限循环,如果答案正确,则不会声明它is@john-我只在帖子中提到了问题。您必须添加代码以指示用户回答正确。如果答案总是错误的,我更新了答案以摆脱无限循环。当答案正确或错误时,它使用所有3个机会,然后进入下一个问题。我希望用户有3次单独的机会。@john-请为我澄清。用户是每个问题有三次机会,还是在整个测验中有三次变化?它要求你每个问题有三次机会
score = 0
print("Question 1. What is 1+1?")
print("a) 2")
print("b) 4")
print("c) 11")
print("d) 3")
x = 0
line = input('Answer: ')
while line != "a":
x = x+1
print('Incorrect, you have used',x,"of your 3 chances")
if x == 3: # if the user uses their three chances, move to the next question.
break
line = input('Answer: ') # try to get another answer
else:
print("You've selected the correct answer")
x = 0 # resets number of chances for the next question
print("Question 2. What is 10x22?")