即使它是对的,Python总是回答错误

时间:2013-12-05 03:54:39

标签: python

如何匹配alist与qlist的答案?每当我尝试测验时,即使它是正确的,也总是说出错误的答案。我如何解决它以使问题与我的答案相符? qlist和alist的匹配,以便当一个人尝试回答时,从alist获得的答案与用户输入的内容匹配并确认它是对还是错。如果它是正确的,将给予用户标记。在此先感谢!!

begin = raw_input ("Press enter to start...")
print " "


q1 = "Jack has 3 balls,\nMary has 5.\nHow many balls they have altogether?"

q2 = "What colour is Blue?\n \n(A) Brown\n(B) BLUE\n(C) coconut"

q3 = "What is 1 + 1 - 1?"

q4 = "What does a time watch do?\n \n(A) Takes you to the future\n(B) Donuts\n(C) Tell the time \nAll of the above"

q5 = "Who is the king of pop?(Micheal Jackson) \n(A) Hillary Duff \n(B) Mr. Jame \n(C) Mr. Sankar \n(D) Micheal Jackson"

q6 = "You see a person dying on the floor from getting shot, what would you do? \n(A)Help him out \n(B) Instagram \n(C) Give him a slice of pizza \n(D) PRAY TO THE CHICKEN MAN"

q7 = "Who is Frankenstein?\n \n" + "(A) DAD!\n" + "(B) Everyone at work\n" + "(C) A novel character\n" + "(D) THE CHICKEN MAN"

q8 = "Which side of your body is your left hand on, when you are looking in a mirror?\n" + "(A) Depends which side of the mirror you are looking at.\n" + "(B) left side\n" + "(c) right side\n" + "(D) Both sides"

q9 = "Jan is twice as old as her sister Betty, but half of Joe's age. Betty just got married. How old is Joe most likely to be?\n" + "(A) 56\n" + "(B) 84\n" + "(C) 1\n" + "(D)2000000"

q10 = "A green man lives in a green house. \nA blue man lives in a blue house. \nWho lives in the white house.\n" + "(A) white man\n" + "(B) black man\n" + "(C) brown man\n" + "(D) Obama"

qlist = (q1, q2, q3, q4, q5, q6, q7, q8, q9, q10)

alist = ("8", "B", "1", "C", "D", "A", "C", "B", "B", "D")

n = 0
value = 0


for question in qlist :
    print question
    str.upper(raw_input(""))

if question == alist[n]:
    print "correct"
    Newvalue = value +10
    print " "
    print Newvalue
    print " "
else:
    print " "
    print "wrong"
    print " "
    n=n+1

1 个答案:

答案 0 :(得分:4)

为什么不将问题和答案存储在一个列表中?

qlist = [(q1, "8"), (q2, "B"), (q3, "1"), (q4, "C"), (q5, "D"), 
         (q6, "A"), (q7, "C"), (q8, "B"), (q9, "B"), (q10, "D")]

然后像这样迭代它们:

for q, a in qlist:
    # q is the question, a is the correct answer

如果您想将它们保存为单独的列表:

for n, q in enumerate(qlist):
    a = alist[n]    # gets the correct answer for current question

但我认为将它们放在一个列表中更简单,因为问题和答案是紧挨着的。实际上,我只是直接定义列表,而不是拥有所有q个变量。