Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
320 views
in Technique[技术] by (71.8m points)

python - ValueError: need more than 1 value to unpack

Ok, i know this topic has been addressed several times, but none of what i seen is helping me. I'm getting the error in the title and i'm not sure how to fix the error. Here is my code:

def loadRecords():
    f = open("stu.txt", "r")
    students = f.readlines()
    f.close()
    return students

def addStudent():
    n = input("Enter student's name: ")
    ex1 = input("Enter Exam 1 grade: ")
    ex2 = input("Enter Exam 2 grade: ")
    ex3 = input("Enter Exam 3 grade: ")
    return n + " " + ex1 + " " + ex2 + " " + ex3 + "
"

def displayStudents(students):
    for record in students:
        n, ex1, ex2, ex3 = record.split(",")
        ex1 = int(ex1)
        ex2 = int(ex2)
        ex3 = int(ex3)
        print("%-10s %5s    %5s    %5s" % (n, ex1, ex2, ex3))

def displayAvg(students):
    n = 1
    for record in students:
        n, ex1, ex2, ex3 = record.split(",")
        ex1 = int(ex1)
        ex2 = int(ex2)
        ex3 = int(ex3)
        avg = (ex1 + ex2 + ex3) / 3
        print("%-10s %5s" % (n, round(avg, 1)))
    n += 1

def saveRecords(students):
    f = open("stu.txt", "w")
    f.writelines(students)
    f.close

def main():
    students = loadRecords()

    while True:
        print("""                         
 Program Options. 
    1.) Display all contacts 
    2.) Create new contact
    3.) Display Averages
    4.) Save and exit 
    """)
        option = input("Enter 1, 2, or 3: ")
        print()

        if option == "1":
            displayStudents(students)
        elif option == "2":
            newRecord = addStudent()
            students.append(newRecord)
        elif option == "3":
            displayAvg(students)
        elif option == "4":
            saveRecords(students)
            break
        else:
            print("Not happening")

main()

Here is the error recieved:

Traceback (most recent call last):
  File "C:/Python33/Program 4/pro4.py", line 65, in <module>
    main()
  File "C:/Python33/Program 4/pro4.py", line 53, in main
    displayStudents(students)
  File "C:/Python33/Program 4/pro4.py", line 16, in displayStudents
    n, ex1, ex2, ex3 = record.split(",")
ValueError: need more than 1 value to unpack

Here is the file I am using, use notepad if you want to run the code.

sam wilson,98,80,73
sue green,92,98,74
sue adams,89,89,92
ron harris,90,87,100
linda tyler,76,72,88
dave smith,72,91,75
steve davis,88,92,84
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You probably have at least one empty line in your file (usually the last line); explicitly test for that:

for record in students:
    if not record.strip():
        continue
    n, ex1, ex2, ex3 = record.split(",")

You may want to look at the csv module to read your student records with instead; you'd still need to skip empty lines but the comma-splitting is handled for you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...