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
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