我已经开始编写多项选择测验,但是我希望Python程序能够随机选择一个用户尚未回答的看不见的问题,这个问题不应该包含以前错误解答的问题。我目前的代码如下:
example.py
print ('WELCOME TO THE MULTIPLE CHOICE TEST\n')
name = input('WHAT IS YOUR NAME? ')
print ('\nHI THERE ' + name + '! LET''S PLAY A GAME!\n')
print ('I will ask you 10 questions and give you three choices for each
question.\n\nYou select which choice is the correct answer. Eg. A, B or
C\n')
print ('\n-----------------------------------------------------------\n')
score = 0
score = int(score) #Convert the 0 into a number so we can add scores
print ('Q1: What is 1 + 1\n')
print ('A. 1')
print ('B. 2')
print ('C. 0')
print ('')
Q1answer = "B"
Q1response= input('Your answer : ').upper()
if (Q1response != Q1answer):
print ('Sorry, that is incorrect!')
else:
print ('Well done! ' + Q1response + ' is correct!')
score = score + 1
print ('Your current score is ' + str(score) + ' out of 10')
print ('\n-----------------------------------------------------------\n')
print ('Q2: What is 0 + 0')
print ('A. 0')
print ('B. 1')
print ('C. -1')
print ('')
Q2answer = "A" or "a"
Q2response= input('Your answer : ').upper()
if (Q2response != Q2answer):
print ('Sorry, that is incorrect!')
else:
print ('Well done! ' + Q2response + ' is correct!')
score = score + 1
print ('Your current score is ' + str(score) + ' out of 10')
print ('\n-----------------------------------------------------------\n')
-------------------------------------------- --------------------------------
修订后的代码:
import random
score = 0
score = int(score)
questions = { # (also contains the answers)
"What is 1 + 1?": "2",
"What is 0 + 0?": "0",
"What is 1 + 2?": "3",
"What is 1 + 3?": "4",
"What is 1 + 4?": "5",
"What is 2 + 1?": "3",
"What is 3 + 2?": "5",
"What is 8 + 2?": "10",
"What is 6 + 0?": "6",
"What is 6 + 1?": "7",
"What is 7 + 1?": "8",
"What is 10 + 0?": "0",
}
learned_questions = set()
while len(questions) > len(learned_questions):
unanswered_questions = set(questions) - learned_questions
chosen_question = random.choice(list(unanswered_questions))
correct_answer = questions[chosen_question]
print (chosen_question)
given_answer = input()
if given_answer == correct_answer:
print ("Correct!")
learned_questions.add(chosen_question)
score = score + 1
print ('Your current score is ' + str(score) + ' out of 10')
print ('\n-----------------------------------------------------------\n')
else:
print ("Wrong! Correct answer would have been:", correct_answer)
print ('Your current score is ' + str(score) + ' out of 10')
print ('\n-----------------------------------------------------------\n')
答案 0 :(得分:2)
收集所有现有问题:
questions = { # (also contains the answers)
"What is 1 + 1?": "2",
"What is 0 + 0?": "0",
}
制作所有正确回答的问题的另一个集合:
learned_questions = set()
通过从两组的差异中做出选择,只选择不属于learned_questions
的问题:
unanswered_questions = set(questions) - learned_questions
chosen_question = random.choice(list(unanswered_questions))
correct_answer = questions[chosen_question]
询问用户的答案:
print chosen_question
given_answer = raw_input()
检查给定答案是否正确,将此问题添加到学习问题的集合中:
if given_answer == correct_answer:
print "Correct!"
learned_questions.add(chosen_question)
else:
print "Wrong! Correct answer would have been:", correct_answer
在所有问题都得到解决之前,将所有这些完成:
while len(questions) > len(learned_questions):
…
现在一起:
questions = { # (also contains the answers)
"What is 1 + 1?": "2",
"What is 0 + 0?": "0",
}
learned_questions = set()
while len(questions) > len(learned_questions):
unanswered_questions = set(questions) - learned_questions
chosen_question = random.choice(list(unanswered_questions))
correct_answer = questions[chosen_question]
print chosen_question
given_answer = raw_input()
if given_answer == correct_answer:
print "Correct!"
learned_questions.add(chosen_question)
else:
print "Wrong! Correct answer would have been:", correct_answer