我一直在研究以下代码,用Python制作一个多选问题程序。我正在寻找一些关于如何提高程序复杂性的建议。
当前程序包含python脚本中的问题和答案集。如何从单独的文件(最好是电子表格或文本文件)中回忆问题和答案?
当前程序随机化所有问题的顺序,并将每个问题的两个错误答案随机化。如何为给定问题分配特定的错误答案,但是随机化了答案的呈现顺序? (例如,如果从电子表格中调用,第1列包含问题,第2列包含正确答案,第3-5列包含错误答案。程序以随机顺序显示所有可能答案后面的问题)
谢谢你和mahalo。
import random
import os
# dictionary containing the information for the questions & answers
word_drills = {'class': 'Tell Python to make a new kind of thing.',
'object': 'Two meanings: the most basic kind of thing, and any instance of some thing.',
'instance': 'What you get when you tell Python to create a class.',
'def': 'How you define a function inside a class.',
'self': 'Inside the functions in a class, self is a variable for the instance/object being accessed.',
'inheritance': 'The concept that one class can inherit traits from another class, much like you and your parents.',
'composition': 'The concept that a class can be composed of other classes as parts, much like how a car has wheels.',
'attribute': 'A property classes have that are from composition and are usually variables.',
'is-a': 'A phrase to say that something inherits from another, as in a Salmon *** Fish',
'has-a': 'A phrase to say that something is composed of other things or has a trait, as in a Salmon *** mouth.'}
# Main portion of the program
def main():
right_answer_total = 0
wrong_answer_total = 0
percentage = 0.0
while True:
print_stats(right_answer_total, wrong_answer_total, percentage)
possible_answers = random.sample(word_drills, 3)
# User is presented with a question. A value from the previous randomly selected possible_answers is selected as the 'correct_answer'
correct_answer = random.choice(possible_answers)
question = word_drills[correct_answer]
print "Question: ", question
print "\n\n(a)%s (b)%s (c)%s" % tuple(possible_answers)
selection = raw_input("> ")
if selection not in ('a', 'b', 'c'):
print "That is not a valid selection."
break
answer = possible_answers[ord(selection) - ord('a')]
if answer == correct_answer:
print "That's correct!"
right_answer_total += 1
else:
print "I'm sorry, that is incorrect..."
wrong_answer_total += 1
percentage = 100 * right_answer_total / float(right_answer_total + wrong_answer_total)
# Stat tracking
def print_stats(right_answer_total, wrong_answer_total, percentage):
os.system('cls' if os.name=='nt' else 'clear')
print "-" * 37
print "| Stat Tracking |"
print "-" * 37
print "| Correct | Incorrect | Percentage |"
print "-" * 37
print "| %d | %d | %d %% |" % (right_answer_total, wrong_answer_total, percentage)
print "-" * 37
print "\n\n\n"
if __name__ == '__main__':
main()
来源:https://codereview.stackexchange.com/questions/14838/multiple-choice-quiz-with-stat-tracking
第一篇文章。对任何论坛虚假的道歉。我会做得更好。
答案 0 :(得分:1)
示例:
from random import choice
word_drills = {
'class': 'Tell Python to make a new kind of thing.',
'object': 'Two meanings: the most basic kind of thing, and any instance of some thing.',
'instance': 'What you get when you tell Python to create a class.',
'def': 'How you define a function inside a class.',
'self': 'Inside the functions in a class, self is a variable for the instance/object being accessed.',
'inheritance': 'The concept that one class can inherit traits from another class, much like you and your parents.',
'composition': 'The concept that a class can be composed of other classes as parts, much like how a car has wheels.',
'attribute': 'A property classes have that are from composition and are usually variables.',
'is-a': 'A phrase to say that something inherits from another, as in a Salmon *** Fish',
'has-a': 'A phrase to say that something is composed of other things or has a trait, as in a Salmon *** mouth.'
}
selected = word_drills[choice(word_drills.keys())]
随机选择10个选项中的3个
keys = word_drills.keys()
choices = []
for k in range(3):
select = choice(keys)
if not select in choices:
choices.append(select)