为什么这不起作用(python)

时间:2017-03-20 02:45:40

标签: python

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?")

意味着给你3次尝试,然后显示答案是否正确。如果第一次或第二次尝试的问题是正确的,也意味着要打破。

1 个答案:

答案 0 :(得分:1)

您需要在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 == 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?")

我希望这能回答你的问题。快乐的编码!