引发文件的 eoferror

发布于 2024-08-14 13:59:36 字数 1317 浏览 3 评论 0原文

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

羅雙樹 2024-08-21 13:59:36

我建议将学生/成绩保存在字典中。如果用户完成输入,将字典pickle放入文件中。喜欢

grades = {}
while True:
    # ask for student's name n
    # ...

    if n.lower() == 'end':
        break

    # ask for student's grade g
    # ...

    grades[n] = g

pickle.dump(grades, yourfile)

I would suggest keeping the students/grades in a dictionary. If the user has finished her input, pickle the dictionary into a file. Like

grades = {}
while True:
    # ask for student's name n
    # ...

    if n.lower() == 'end':
        break

    # ask for student's grade g
    # ...

    grades[n] = g

pickle.dump(grades, yourfile)
牛↙奶布丁 2024-08-21 13:59:36

您在程序预期之前就遇到了文件“EOF”的末尾。

You're running into the end of the file "EOF" before your program expects to.

蔚蓝源自深海 2024-08-21 13:59:36

您遇到错误全局名称'names'未定义,因为您已在def entoles()内声明了names并且仅在此范围内可见。

如果您希望能够访问它,则必须在 dev entoles() 之外声明 names

另外,

x=names.append(name)
f.write(str(x))

append 是一种将传递到列表的项目就地追加的方法。该方法返回“无”
因此,f.write(str(x)) 将写入“None”。

编辑:
关于 f.read() 和 此 Python 文档 用于输入/raw_input

You have the error global name 'names' is not defined because you have declare names within def entoles() and is only seen within this scope.

You have to declare names outside the dev entoles() if you want to be able to access it.

Also

x=names.append(name)
f.write(str(x))

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

寒江雪… 2024-08-21 13:59:36

问题在于 myfunc 函数在每次迭代时重新打开文件,并删除其内容。您应该将 open 调用移至 while 循环之前。

The problem is that the myfunc function reopens the file on each iteration, deleting its contents. You should move the open call to before the while loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文