views:

104

answers:

4

EDITED:

 import pickle
filename=input('Enter a file name:')



def entoles():
    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 "C:/Users/Ειρήνη -2-/Desktop/irstavr Exercise1 Seira3.py", line 68, in 
  File "C:/Users/Ειρήνη -2-/Desktop/irstavr Exercise1 Seira3.py", line 45, in syn2
NameError: global name 'names' is not defined

why does it happens? How can i fix it? I have made many changes over and over...but the result stays the same...it doesnt work! :(

A: 

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

md5sum
So,What I have to do? Sorry...But I'm a beginner. This may seems rediculous to you...but i cant fix it! :((sorry for the 'answer' Im also new on the site!)
FILIaS
see jellybean's answer for a better solution all around. +1 jellybean
md5sum
+1  A: 

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)
jellybean
if i do this:Give a student's name:faeGive student's grade:2Give a student's name:rgGive student's grade:2Give a student's name:aeweGive student's grade:3Give a student's name:feagGive student's grade:5Give a student's name:geaGive student's grade:3Give a student's name:endFile closedBack to Menu.... .... then the dictionary is like that:{'fae': {'gae': {...}, 'fae': {...}}}
FILIaS
Hm ... I'd need to see that code
jellybean
take a look to the edited one...its not what you suggested...but i think it's similar! ;)
FILIaS
Doesn't even look close to me... But I'm not all THAT well versed in python syntax.
md5sum
A: 

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.

interjay
well now i dont have EOFError but the dictionary contains only the 1st student and the grade!:(
FILIaS
That's because you only `pickle.load` the first student. You can fix this by loading repeatedly, but it's better and easier to follow jellybean's suggestion and pickle the dictionary.
interjay
but why it's only the first?...picle.load is on the loop...and name changes each time :\
FILIaS
There is no loop in `syn2`, so only the first one is loaded.
interjay
yes...but from the first one...the names are loades all together.Why it takes only the 1st? xmmmm
FILIaS
can you give me an idea of what you mean?Thnx in advance
FILIaS
A: 

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

ccheneson
well...i declare names outside the 1st function but now i have this:TypeError: 'list' object cannot be interpreted as an integer
FILIaS
From the python doc:"To read a file’s contents, call f.read(size), which reads some quantity of data and returns it as a string. size is an optional numeric argument. When size is omitted or negative, the entire contents of the file will be read and returned;"And what you currently do is to pass a list to read() instead of a integer(size).
ccheneson
Also, you may want to use 'raw_input' instead of 'input'. See the docs referring to those methods in my edit.
ccheneson