Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/svn/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么这不起作用(python)_Python - Fatal编程技术网

为什么这不起作用(python)

为什么这不起作用(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

意味着给你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
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?")