【发布时间】:2015-03-28 03:44:07
【问题描述】:
我一直在研究以下代码,以使用 Python 制作多项选择题程序。我正在寻找一些关于如何增加程序复杂性的建议。
当前程序包含 python 脚本中的问题和答案集。如何从单独的文件(最好是电子表格或文本文件)中回忆问题和答案?
-
当前程序随机化所有问题的顺序,并随机化每个问题的两个错误答案。如何为给定的问题分配特定的错误答案,但随机化给出答案的顺序? (例如,如果从电子表格中调用,第 1 列包含问题,第 2 列包含正确答案,第 3-5 列包含错误答案。程序以随机顺序显示问题,然后是所有可能的答案)
谢谢你和马哈洛。
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
第一次发帖。对任何论坛虚假暂停表示歉意。我会做得更好。
【问题讨论】:
标签: python python-2.7 google-sheets