Python 随机测验生成器

Python 随机测验生成器,python,dictionary,matplotlib,graphing,Python,Dictionary,Matplotlib,Graphing,我的代码有问题,我不知道如何获得问题的随机样本。这是代码,它一直给我一个关键错误。我试图从问题列表中随机打印一个问题,但它告诉我问题是一本字典。有人能帮我吗 这是密码 #import matplotlib.pyplot as plt import random, math, csv def read_database(filename): dictionary = {} infile = open(filename, 'r') for line in infile:

我的代码有问题,我不知道如何获得问题的随机样本。这是代码,它一直给我一个关键错误。我试图从问题列表中随机打印一个问题,但它告诉我问题是一本字典。有人能帮我吗

这是密码

#import matplotlib.pyplot as plt
import random, math, csv

def read_database(filename):
    dictionary = {}

    infile = open(filename, 'r')
    for line in infile:
        line = line.strip()
        q,a1,a2,a3,a4 = line.split(',')
        question = (q)
        answers = [a1,a2,a3,a4]
        dictionary[question] = answers
    infile.close()
    return dictionary

def read_results(outfile, score):
    player = {}
    outfile = open(outfile, "w")
    name = input("What is your name?")
    player[name] = score
    print(score, file = outfile)

    outfile.close()

def ask_question(question, answers):
    score = 0
    print(question[0])
    for multi in answers[1:5]:
        print(multi)
    answer = input("Please select an answer: ")
    print()
    if answer == answer[0]:
        print("Correct!")
        score += 1
    else:
        print("Incorrect! - the correct answer was {0}.".format(answer[0]))
    print()
    return score

def main():
    questions = read_database("data.csv")

    score = 0
    score = int(score)
    print()
    print()
    print("=============================")
    print("Welcome to the baseball quiz!")
    print("=============================")
    print()
    print()
    name = input("What is your name?")
    number = int(input("Hi," + name + " there are {0} questions - how many do you want in your quiz: ".format(len(questions))))
    if number > len(questions):
        print("The quiz only has 10 questions, you will be asked 10 questions. ")
    else:
        print("You will be asked", number, "quesitons")
    key_list = random.sample(questions.keys(), number)
    #print(key_list)
    score = 0
    for key in key_list:
        print(key, questions.get(key))
        score = ask_question(questions, key)
        print(score)

    print("Your final score was {0} out of {1}.".format(score,number), score/100)

if __name__ == "__main__":
    main()
这是csv文件中我的数据示例:

谁赢得了今年的世界大赛?小熊队,大都会队,红衣主教队,印第安人队

有多少人在场上防守?、九、六、十、二十

棒球比赛中有多少局?九、十、七、六< /P>
函数
ask\u question(问题,答案)
需要获取问题和可能答案列表作为参数

所以你需要这样称呼它

score = ask_question( key, questions.get(key))

在这种情况下,
key
是问题,可能的答案列表是
问题。get(key)

您正在呼叫
ask_questions
questions
,但似乎只需要一个问题和答案。