我想創建一個數學遊戲,它隨機從我的列表中選擇一個問題並給出相應的答案。目前,我可以將問題隨機發現,但它從列表中依次給出答案,而不是與問題相對應的答案。 我如何得到答案是正確的隨機問題。 這是我的代碼。隨機選擇一個公式,並從列表中回答 - PYTHON
import random
counter = 0
score = 0
incorrect = 0
name=input("What is your name?")
print("Hi",name,",welcome to your math quiz!")
questions = ["10*2","4-2","6+12","6*4","12-5","6+54","1*0","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)
for idxq, question in idx_questions:
print(question)
ans = input("What is the answer? ")
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", answers)
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,"%")
請編輯您的文章並添加實際的代碼,而不是鏈接屏幕截圖。這有助於用戶閱讀和理解你的代碼,並允許複製它進行測試。 – Hexaholic