python - 尽管似乎已定义,但为什么代码会收到行错误
问题描述
我有一个grade.txt,其中包括
Mickey,Mouse,90*
Jane,Doe,50
Minnie,Mouse,95
Donald,Duck,80
Daffy,Duck,70
尝试读取文件并拆分以在计算平均值时将名称和等级分开。我正在接收未定义的线路。
def main ():
with open('grade.txt' , 'r') as f:
lines = f.readlines(); #reads all the line in file to lines list.
f.close(); #closes f stream.
sum = 0; #initialised sum to store the sum of all grades.
print('Name Grade');
print('--------------------');
for line in lines: #loops through the lines list
str = line.split(','); #splits each line based on ','
grade = int(str[2]); #converts string to numeric value.
sum += grade; #adds the grade value to sum.
print(str[0]," ",str[1]," ",grade); #prints the line read after splitting.
print('Average Grade: ',round((sum/len(lines)),1)); #prints average of all grades by rounding it to 1 place.
newdata = input('Enter firstname,lastname and grade: '); #prompts user.
with open('grade.txt', 'a') as f: #opens file in append mode.
f.write('\n'+newdata); #writes data into file in new line.
f.close(); #closes f stream
main ()
对正确方向的帮助表示赞赏。
错误给了
Traceback (most recent call last):
File "main.py", line 13, in <module>
for line in lines: #loops through the lines list
NameError: name 'lines' is not defined
编辑代码
def main ():
with open('grades.txt' , 'r') as f:
lines = f.readlines(); #reads all the line in file to lines list.
sum = 0; #initialised sum to store the sum of all grades.
print('Name Grade');
print('--------------------');
for line in lines: #loops through the lines list
str = line.split(','); #splits each line based on ','
grade = int(str[2]); #converts string to numeric value.
sum += grade; #adds the grade value to sum.
print(str[0]," ",str[1]," ",grade); #prints the line read after splitting.
print('Average Grade: ',round((sum/len(lines)),1)); #prints average of all grades by rounding it to 1 place.
newdata = input('Enter firstname,lastname and grade: '); #prompts user.
with open('grades.txt', 'a') as f: #opens file in append mode.
f.write('\n'+newdata); #writes data into file in new line.
f.close(); #closes f stream
谢谢,我通过了带有缩进的行错误并删除了 f.close; 通过我看到我的代码没有输出文件的内容。它只打印新数据
解决方案
当你创建一个类似的变量时lines
,它只能在创建它的函数中使用。
您的 main 函数实际上在 line 之后结束f.close();
。要使所有内容成为您的主要功能的一部分,您需要保持相同级别的缩进。
def main ():
with open('grade.txt' , 'r') as f:
lines = f.readlines(); #reads all the line in file to lines list.
f.close(); #closes f stream.
sum = 0; #initialised sum to store the sum of all grades.
print('Name Grade');
print('--------------------');
for line in lines: #loops through the lines list
str = line.split(','); #splits each line based on ','
grade = int(str[2]); #converts string to numeric value.
sum += grade; #adds the grade value to sum.
print(str[0]," ",str[1]," ",grade); #prints the line read after splitting.
print('Average Grade: ',round((sum/len(lines)),1)); #prints average of all grades by rounding it to 1 place.
newdata = input('Enter firstname,lastname and grade: '); #prompts user.
with open('grade.txt', 'a') as f: #opens file in append mode.
f.write('\n'+newdata); #writes data into file in new line.
f.close(); #closes f stream
main ()
您的代码有几个指针。在 Python 中,每行末尾不需要分号。
f.close()
使用with
块时不需要该行。该文件在块的末尾自动关闭with
。
推荐阅读
- python - 我收到 TypeError: list indices must be integers or slices, not float for this code
- mysql - Mysql:知道一个类别中的所有产品是否都处于非活动状态
- apache-spark - spark 3.0- spark 聚合函数给出与预期不同的表达式
- mysql - MySQL如何通过Where子句获得具有一对多关系的结果
- api - 使用 Azure Active Directory (AAD) 令牌时对非默认 Yammer 网络使用 Yammer API
- python - 为每个数组调用函数打印python
- botframework - 如何在 azure bot 模拟器中调试语音?如果我选中“在调试中打开”,直接字段将被隐藏,我无法启用语音
- excel - 从 Outlook 运行 Excel 宏并将值返回到 Outlook
- jpos - JPOS 中的默认空间大小是多少以及如何增加 JPOS 中的默认空间大小?
- javascript - Material UI Tabs:在标签之间切换后,标签中的更改被丢弃