Python 为什么文件名不是搁置模块可以打开的数据库?

Python 为什么文件名不是搁置模块可以打开的数据库?,python,random,shelve,Python,Random,Shelve,我的代码中的搁置模块部分不起作用,我不理解错误的意思。我试图将最后三个分数保存到一个学生的名字中,然后用各种方式对其排序。学生完成测验后会出现错误。尝试删除文件名处的文件,然后再次运行脚本,以便脚本负责以正确的格式创建数据库文件。如果您要求它打开一个空文件或通过其他方式(如CSV、JSON等)创建的文件,则它不知道如何处理该文件。“我不明白错误的意思”*当你以学生身份参加测验后,出现错误抱歉,请回答你的问题并添加错误文本。错误表明,无论文件名是什么,它都不是shelve知道如何打开的数据库。当我

我的代码中的搁置模块部分不起作用,我不理解错误的意思。我试图将最后三个分数保存到一个学生的名字中,然后用各种方式对其排序。学生完成测验后会出现错误。

尝试删除
文件名处的文件,然后再次运行脚本,以便脚本负责以正确的格式创建数据库文件。如果您要求它打开一个空文件或通过其他方式(如CSV、JSON等)创建的文件,则它不知道如何处理该文件。

“我不明白错误的意思”*当你以学生身份参加测验后,出现错误抱歉,请回答你的问题并添加错误文本。错误表明,无论
文件名是什么,它都不是shelve知道如何打开的数据库。当我这样做时,它会出现另一个错误,说明文件名未定义否,删除计算机上具有
filename
等于的任何名称的实际文件。不要更改python代码。你仍然需要这个变量。它现在说db类型无法确定。可能我解释得不清楚,所以试试这个。将
shelve.open(文件名)
替换为
shelve.open('dbfile')
。不要自己创建名为
dbfile
的文件(甚至不是空文件)——脚本必须从头开始创建文件。运行脚本后,将创建文件。它不会是人类可读的格式,脚本中手动打开和处理文件的其他部分也不会工作。您必须只使用
shelve
来处理该文件,或者根本不使用它
import random
import sys
import shelve

def get_input_or_quit(prompt, quit="Q"):
    prompt += " (Press '{}' to exit) : ".format(quit)
    val = input(prompt).strip()
    if val.upper() == quit:
        sys.exit("Goodbye")
    return val

def prompt_bool(prompt):
    while True:
        val = get_input_or_quit(prompt).lower()
        if val == 'yes':
          return True
        elif val == 'no':
          return False
        else:
         print ("Invalid input '{}', please try again".format(val))


def prompt_int_small(prompt='', choices=(1,2)):
    while True:
        val = get_input_or_quit(prompt)
        try:
            val = int(val)
            if choices and val not in choices:
                raise ValueError("{} is not in {}".format(val, choices))
            return val
        except (TypeError, ValueError) as e:
                print(
                    "Not a valid number ({}), please try again".format(e)
                    )

def prompt_int_big(prompt='', choices=(1,2,3)):
    while True:
        val = get_input_or_quit(prompt)
        try:
            val = int(val)
            if choices and val not in choices:
                raise ValueError("{} is not in {}".format(val, choices))
            return val
        except (TypeError, ValueError) as e:
                print(
                    "Not a valid number ({}), please try again".format(e)
                    )

role = prompt_int_small("Are you a teacher or student? Press 1 if you are a student or 2 if you are a teacher")
if role == 1:
    score=0
    name=input("What is your name?")
    print ("Alright",name,"welcome to your maths quiz."
            " Remember to round all answers to 5 decimal places.")
    level_of_difficulty = prompt_int_big("What level of difficulty are you working at?\n"
                                 "Press 1 for low, 2 for intermediate "
                                    "or 3 for high\n")


    if level_of_difficulty == 3:
        ops = ['+', '-', '*', '/']
    else:
        ops = ['+', '-', '*']

    for question_num in range(1, 11):
        if level_of_difficulty == 1:
            max_number = 10
        else:
            max_number = 20

        number_1 = random.randrange(1, max_number)
        number_2 = random.randrange(1, max_number)
        operation = random.choice(ops)

        maths = round(eval(str(number_1) + operation + str(number_2)),5)
        print('\nQuestion number: {}'.format(question_num))
        print ("The question is",number_1,operation,number_2)
        answer = float(input("What is your answer: "))
        if answer == maths:
            print("Correct")
            score = score + 1
        else:
            print ("Incorrect. The actual answer is",maths)

    if score >5:
        print("Well done you scored",score,"out of 10")
    else:
        print("Unfortunately you only scored",score,"out of 10. Better luck next time")


    class_number = prompt_int_big("Before your score is saved ,are you in class 1, 2 or 3? Press the matching number")

    filename = (str(class_number) + "txt")
    with shelve.open(filename) as db: # This is the effected line
        old_scores = db.get(name, [])
        old_scores.extend(score)
        db[name] =score[-3:]


    if prompt_bool("Do you wish to view previous results for your class"):
        for line in lines:
            print (line)
    else:
        sys.exit("Thanks for taking part in the quiz, your teacher should discuss your score with you later")

if role == 2:
    class_number = prompt_int_big("Which class' scores would you like to see? Press 1 for class 1, 2 for class 2 or 3 for class 3")
    filename = (str(class_number) + "txt")
    sort_or_not = int(input("Would youlike to sort these scores in any way? Press 1 if the answer is no or 2 if the answer is yes"))
    if sort_or_not == 1:
        f = open(filename, "r")
        lines = [line for line in f if line.strip()]
        lines.sort()
        for line in lines:
            print (line)
    if sort_or_not == 2:
        type_of_sort = int(input("How would you like to sort these scores? Press 1 for scores in alphabetical order with each student's highest score for the tests, 2 if you would like to see the students' highest scores sorted from highest to lowest and 3 if you like to see these student's average scores sorted from highest to lowest"))
        if type_of_sort == 1:
            with open(filename , 'r') as r:
                for line in sorted(r):
                    print(line, end='')
        if type_of_sort == 2:
            file = open(filename, 'r')
            lines = file.read().splitlines()
            s = {lines[i]:[int(k) for k in lines[i+1:i+4]] for i in range(0,len(lines),4)}
            for i in sorted(s.keys()):
                print (i,max(s[i]))

            avg_mark = lambda name:sum(s[name])/len(s[name])
            for i in sorted(s.keys(),key=avg_mark,reverse=True):
                print (i,avg_mark(i))
        if type_of_sort == 3:
            file = open(filename, 'r')
            lines = file.read().splitlines()
            s = {lines[i]:[int(k) for k in lines[i+1:i+4]] for i in range(0,len(lines),4)}
            for i in sorted(s.keys()):
                print (i,max(s[i]))

            high_mark = lambda name:max(s[name])
            for i in sorted(s.keys(),key=high_mark,reverse=True):
                print (i,high_mark(i))