Rose
Rose

Reputation: 127

How can I fix this "ValueError: not enough values to unpack (expected 3, got 2)"?

Please help me with my assignment!

My goal is that the user can choose whose grade he/she wants to see in a certain subject. The output should be something like this:

Name: Mary , Math Grade: 98

This is what I have so far:

N = int(input("Please enter number of students: "))

while (N < 2 or N > 10):
    N = int(input("Please only enter 2 - 10 students: "))

name = []
math_grade = []
science_grade = []

while (N != 0):
    name.append(input('Enter name:'))
    math_grade.append(input('Enter grade in math:'))
    science_grade.append(input('Enter grade in science:'))
    N = N - 1

record = []    
record.append(name)
record.append(math_grade)
record.append(science_grade)

Class_Record = {}

for name, math_grade, science_grade in record:
    Class_Record[name] = {"name": name, "math grade": math_grade, "science grade": science_grade }

I keep getting the value error at that point. I also don't know how to print it to get what I want.

If you can help me fix the error and tell me how I can print my desired output, I would be really grateful!!!

Upvotes: 0

Views: 72

Answers (3)

John Gordon
John Gordon

Reputation: 33285

The problem is how you've organized the record list.

This line of code expects each item in record to be a sub-list with three items: name, math grade, and science grade.

for name, math_grade, science_grade in record

So it's expecting record to be something like this:

record = [
    ["Jim", "B", "C"],
    ["Mary", "A", "B"],
]

But instead, you made record to be a sub-list of names, then a sub-list of math grades, then a sub-list of science grades:

record = [
    ["Jim", "Mary"],
    ["B", "A"],
    ["C", "B"]
]

So when the code pulls the first item in record, it's a list of only two items, where you told it to expect three. (If you had three students, the code would have "worked", but not the way you wanted.)

So you either have to change how you put things into record to match how you're pulling them out, or change how you're pulling them out.

In fact, it seems like record is just a temporary variable that is only used to build Class_Record. If that's true, you could just build Class_Record directly:

Class_Record = {}

while (N != 0):
    name = input('Enter name:')
    math_grade = input('Enter grade in math:')
    science_grade = input('Enter grade in science:')
    Class_Record[name] = {'math_grade': math_grade, 'science_grade': science_grade)
    N = N - 1

Upvotes: 1

ck22
ck22

Reputation: 276

You can change your code like below

N = int(input("Please enter number of students: "))

while (N < 2 or N > 10):
    N = int(input("Please only enter 2 - 10 students: "))


Class_Record={}
while (N != 0):
    name = input('Enter name:')
    math_grade = input('Enter grade in math:')
    science_grade = input('Enter grade in science:')
    Class_Record[name]={"name": name, "math grade": math_grade, "science grade": science_grade}
    N = N - 1

print(Class_Record)

Upvotes: 0

bashBedlam
bashBedlam

Reputation: 1500

N = int(input("Please enter number of students: "))

while (N < 2 or N > 10):
    N = int(input("Please only enter 2 - 10 students: "))

Class_Record = []

while (N != 0):
    name = input('Enter name:')
    math_grade = input('Enter grade in math:')
    science_grade = input('Enter grade in science:')
    temp = {'name': name, 'math grade': math_grade, 'science grade': science_grade}
    Class_Record.append (temp)
    N = N - 1

print (Class_Record)

Upvotes: 0

Related Questions