【问题标题】:Mutliple Choice Program and iterations (2.7)多项选择程序和迭代 (2.7)
【发布时间】:2015-03-28 03:44:07
【问题描述】:

我一直在研究以下代码,以使用 Python 制作多项选择题程序。我正在寻找一些关于如何增加程序复杂性的建议。

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

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


    【解决方案1】:

    看看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)
    

    【讨论】:

    • 谢谢你,毛罗。我仍然无法弄清楚如何从电子表格而不是 Python 字典中引入数据。
    • 我目前正在使用 googledoc 电子表格,但如果有更好的选择,我不会承诺使用它们。我刚刚开始研究数据库类型及其各自的用途。
    猜你喜欢
    • 1970-01-01
    • 2018-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多