引发文件的 eoferror
import pickle
filename=input('Enter a file name:')
def commands():
f=open(filename,'w')
names=[]
grades=[]
while True:
name=input("Give a student's name:")
if name.lower()=='end':
f.close()
print("File closed")
print("Back to Menu")
break
else:
x=names.append(name)
f.write(str(x))
grade=input("Give student's grade:")
try:
grade=float(grade)
if 0<=grade<=10:
y=grades.append(grade)
f.write(str(y))
else:
print("Please give a grade!Between 0-10! ")
except ValueError:
print(grade,"is not a number...Give only Numbers!")
def syn2(filename):
try:
f=open(filename,'r')
f.read(names)
f.read(grades)
d1={}
d1[names]=grades
print(d1)
print("Back to Menu")
except IOError:
return False
当我调用 syn2(文件名) 时:
Traceback (most recent call last): File "file1.py", line 68, in File "file2.py", line 45, in syn2 NameError: global name 'names' is not defined
import pickle
filename=input('Enter a file name:')
def commands():
f=open(filename,'w')
names=[]
grades=[]
while True:
name=input("Give a student's name:")
if name.lower()=='end':
f.close()
print("File closed")
print("Back to Menu")
break
else:
x=names.append(name)
f.write(str(x))
grade=input("Give student's grade:")
try:
grade=float(grade)
if 0<=grade<=10:
y=grades.append(grade)
f.write(str(y))
else:
print("Please give a grade!Between 0-10! ")
except ValueError:
print(grade,"is not a number...Give only Numbers!")
def syn2(filename):
try:
f=open(filename,'r')
f.read(names)
f.read(grades)
d1={}
d1[names]=grades
print(d1)
print("Back to Menu")
except IOError:
return False
WHEN I CALL syn2(filename) :
Traceback (most recent call last): File "file1.py", line 68, in File "file2.py", line 45, in syn2 NameError: global name 'names' is not defined
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我建议将学生/成绩保存在字典中。如果用户完成输入,将字典
pickle
放入文件中。喜欢I would suggest keeping the students/grades in a dictionary. If the user has finished her input,
pickle
the dictionary into a file. Like您在程序预期之前就遇到了文件“EOF”的末尾。
You're running into the end of the file "EOF" before your program expects to.
您遇到错误
全局名称'names'未定义
,因为您已在def entoles()
内声明了names
并且仅在此范围内可见。如果您希望能够访问它,则必须在
dev entoles()
之外声明names
。另外,
append 是一种将传递到列表的项目就地追加的方法。该方法返回“无”
因此,
f.write(str(x))
将写入“None”。编辑:
关于 f.read() 和 此 Python 文档 用于输入/raw_input
You have the error
global name 'names' is not defined
because you have declarenames
withindef entoles()
and is only seen within this scope.You have to declare
names
outside thedev entoles()
if you want to be able to access it.Also
append is a method that append the item passed to the list in-place. The method returns 'None'
Therefore,
f.write(str(x))
will write 'None'.EDIT:
Python doc about Input/Output for f.read() and this Python doc for input/raw_input
问题在于 myfunc 函数在每次迭代时重新打开文件,并删除其内容。您应该将
open
调用移至 while 循环之前。The problem is that the
myfunc
function reopens the file on each iteration, deleting its contents. You should move theopen
call to before the while loop.