首页 > 解决方案 > 尽管似乎已定义,但为什么代码会收到行错误

问题描述

我有一个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; 通过我看到我的代码没有输出文件的内容。它只打印新数据

标签: python

解决方案


当你创建一个类似的变量时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


推荐阅读