Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在文件中搜索特定名称,然后使用函数更改与之关联的值_Python_String_Function_Dictionary - Fatal编程技术网

Python 在文件中搜索特定名称,然后使用函数更改与之关联的值

Python 在文件中搜索特定名称,然后使用函数更改与之关联的值,python,string,function,dictionary,Python,String,Function,Dictionary,我的程序创建的文件列出了班级中的学生及其分数。我正在尝试制作一个函数,允许用户在相应的类文件中编辑特定学生的分数。到目前为止,我已经能够创建文件并添加学生及其分数,如下所示: def createClass(chosenclass, data): classoptions = 'Class {}'.format(chosenclass) with open(f'{classoptions}.txt', '+w') as file: for key,value in

我的程序创建的文件列出了班级中的学生及其分数。我正在尝试制作一个函数,允许用户在相应的类文件中编辑特定学生的分数。到目前为止,我已经能够创建文件并添加学生及其分数,如下所示:

def createClass(chosenclass, data):
    classoptions = 'Class {}'.format(chosenclass)
    with open(f'{classoptions}.txt', '+w') as file:
        for key,value in data.items():
            studentdata = 'Name: {}, Score: {}/100\n'.format(key, value)
            file.write(studentdata)
    return file


command = input('Please enter a command: ')
while command:
    if command == '6':
        print('Have a good day!')
        exit()
    elif command == '1': # opt 1
        print('\n')
        askclass = input('Please enter your class: ')
        studentno = int(input('How many student\'s are in your class?: '))
        data = {}
        for i in range(0,studentno):
            askstudent = input('Please enter a student\'s full name: ')
            askscore = int(input('Please enter the student\'s score: '))
            data[askstudent] = askscore
            print('\n')
            print('Student Added!')
            createClass(askclass,data)
        command = input('Please enter a command: ')
现在,我正在尝试创建一个函数,允许您编辑任何学生的分数

为此,我尝试使用.split属性创建一个列表,其中包含每行中每个学生的姓名和分数,然后将该列表转换为字典。到目前为止,我已经得出了以下结论:

def convertList(listname):
    iteration = iter(listname) 
    resultant = dict(zip(iteration, iteration)) 
    return resultant

def editStudent(classfile, selectedstudent, newscore):
    with open(f'Class {classfile}.txt', '+w') as chosen:
        for line in chosen:
            listver = line.split(',')
        data = convertList(listver)
        for key, value in data.items():
            if key == 'Name: ' + str(selectedstudent):
                formattedresult = 'Score: {}/100'.format(newscore)
                value.replace(value, formattedresult)
                final = print('New result is ' + newscore + '/100')
                return final

if command == '3':
        whichfile = input('Which class would you like to edit?: ')
        whichstudent = input('Which student would you like to edit?: ')
        whichscore = input('Please enter the updated score: ')
        editStudent(whichfile,whichstudent,whichscore)
        command = input('Please enter a command: ')
但是,当我运行此操作时,会出现以下错误:

Traceback (most recent call last):
  File "/Users/sinclair_ma/Desktop/IST AT1/testing.py", line 90, in <module>
    editStudent(whichfile,whichstudent,whichscore)
  File "/Users/sinclair_ma/Desktop/IST AT1/testing.py", line 48, in editStudent
    data = convertList(listver)
UnboundLocalError: local variable 'listver' referenced before assignment
回溯(最近一次呼叫最后一次):
文件“/Users/sinclair_ma/Desktop/IST AT1/testing.py”,第90行,在
编辑学生(whichfile,whichstudent,whichcore)
文件“/Users/sinclair_ma/Desktop/IST AT1/testing.py”,第48行,学生版
数据=转换列表(listver)
UnboundLocalError:赋值前引用的局部变量“listver”

我有一种感觉,我在尝试替换这些值时走错了方向,但我很难找到任何替代方法。如果能帮我找到正确的方向,我们将不胜感激。

请始终将完整的错误信息(从单词“Traceback”开始)作为文本(而不是截图)进行讨论(而不是评论)。还有其他有用的信息。如果在函数中您想分配给全局变量,则必须使用word
global
通知函数使用全局变量,而不是创建局部变量。我认为您的缩进错误。您运行
listver=line.split(',')
inside
for
-loop,但稍后您使用
data=convertList(listver)
exterloop,因此它只能在最后一个
listver
或没有
listver
时运行。