这是我的代码片段。它一直说用户不正确。每个问题的答案在列表中的顺序相同。
questions = ["45 - 20","32 - 12","54 + 41"]
# Answer Bank
answers = ["25","20","95"]
while qleft != 0:
# Get random question
qnumber = randint(0,2)
currentquestion = questions[qnumber]
currentanswer = answers[qnumber]
# Question
userchoice = int(input("What does {} equal? ".format(currentquestion)))
# Check Answer
if userchoice != currentanswer:
print("Incorrect")
qleft -= 1
elif userchoice == currentanswer:
print("Correct")
qleft -= 1
else:
print("That's not a valid answer")
答案 0 :(得分:2)
您正在将int
输入与str
答案进行比较,这些答案永远不会相等。
如果您想处理整体,请更改
answers = ["25","20","95"]
到
answers = [25,20,95]
并且它将起作用。
或者,请勿将input()
的结果转换为int
。
答案 1 :(得分:1)
用户的答案userchoice
是一个整数,但实际答案currentanswer
是一个字符串。如果您只是将输入保留为字符串,它将起作用:
questions = ["45 - 20","32 - 12","54 + 41"]
# Answer Bank
answers = ["25","20","95"]
while qleft != 0:
# Get random question
qnumber = randint(0,2)
currentquestion = questions[qnumber]
currentanswer = answers[qnumber]
# Question
userchoice = input("What does {} equal? ".format(currentquestion))
# Check Answer
if userchoice != currentanswer:
print("Incorrect")
qleft -= 1
elif userchoice == currentanswer:
print("Correct")
qleft -= 1
else:
print("That's not a valid answer")
答案 2 :(得分:0)
区别在于:
import random as r
questions = ["45 - 20","32 - 12","54 + 41"]
# Answer Bank
answers = [25,20,95]
qleft = 3
while qleft != 0:
# Get random question
qnumber = r.randint(0,2)
currentquestion = questions[qnumber]
currentanswer = answers[qnumber]
# Question
userchoice = int(input("What does {} equal? ".format(currentquestion)))
# Check Answer
if userchoice != currentanswer:
print("Incorrect")
qleft -= 1
elif userchoice == currentanswer:
print("Correct")
qleft -= 1
else:
print("That's not a valid answer")
注意" 25" != 25
答案 3 :(得分:0)
由于您要将用户的答案转换为int,因此正确的答案也必须是int。否则,您将字符串与int进行比较,它永远不会相等。
Label2 : Monday,10:00,6:00
label1 : Sunday,Closed,Closed Monday,10:00,6:00 Tuesday,10:00,8:00 Wednesday,10:00,6:00 Thursday,10:00,6:00 Friday,10:00,6:00 Saturday,10:00,5:00