显示列表中的答案

时间:2017-10-13 13:05:05

标签: python-3.x

在我的代码的最后一行。我正在显示问题,用户回答和正确的答案。但是,当我在最后添加答案部分时,它给出了一个超出范围错误的索引。我似乎无法解决这个问题。有人可以帮忙吗? 谢谢

import random

counter=0
score = 0
incorrect = 0

name=input("What is your name?")
print("Hi",name,",welcome to your math quiz!")

questions = ["10x2","4-2","6+12","6x4","12-5","6+54","1x0","3-6","4+0","65-9"]
answers=["20","2","18","24",'7','60','0','-3','4','56']

idx_questions = list(enumerate(questions))
idx_answers = list(enumerate(answers))

random.shuffle(idx_questions)

counter=0
inputs = []
for idxq, question in idx_questions:
    print()
    print("Question",counter+1,":",question)
    print()
    ans = input("What is the answer? ")
    counter=counter+1

    inputs.append(ans)
    for idxa, answer in idx_answers:
        if idxq == idxa and ans == answer:
            print("Correct")
            score=score+1

            print("Correct Answers=",score)
            print("Incorrect Answers=",incorrect)

        elif idxq == idxa and ans != answer:
            print("Incorrect. The answer is", answer)
            incorrect=incorrect+1
            print("Correct Answers=",score)
            print("Incorrect Answers=",incorrect)

print("End of quiz")
print(name,"your score is",score,"out of 10")
print(score*10,"/100")
print(score*10,"%")
counter=0
while counter<10:
    for idxq, question in idx_questions:
            print("Question",counter+1,":",question,": Your answer =", inputs[counter],"Correct Answer =",answer)
            counter=counter+1

1 个答案:

答案 0 :(得分:1)

尝试这样做,不要担心索引,你可以在你想要的地方添加风味文本:

from random import shuffle

questions = ["10x2","4-2","6+12","6x4","12-5","6+54","1x0","3-6","4+0","65-9"]
answers = ["20","2","18","24",'7','60','0','-3','4','56']
combo = dict(zip(questions, answers))

shuffle(questions)
score = 0
listing = []

for q in questions:
  print(q)
  ans = input()
  if ans == combo[q]:
    score += 1
  listing.append((q, ans, combo[q]))

print(score / 10)
for item in listing:
  print('For question {} you answerd {} and correct answer is {}'.format(*item))