Skip to main content
 首页 » 编程设计

python之多项选择计划和迭代 (2.7)

2025年05月04日8虾米姐

我一直在研究下面的代码,用Python做一个选择题程序。我正在寻找有关如何增加程序复杂性的建议。

  1. 当前程序包含 python 脚本中的问题和答案集。如何从单独的文件(最好是电子表格或文本文件)中记忆问题和答案?

  2. 当前程序将所有问题的顺序随机化,并随机化每个问题的两个错误答案。我怎样才能为给定的问题分配特定的错误答案,但随机出现答案的顺序? (例如,如果从电子表格中记忆起,第 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

第一次发帖。对任何论坛失态暂停表示歉意。我会做得更好。

请您参考如下方法:

看看random.choice

例子:

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)