-1

This is exercise 38 of part 4 of the Python Programming Course from MOOC.fi (https://programming-23.mooc.fi/part-4/6-strings-and-lists - the last one on this site). I tried solving it, but the error in the title keeps appearing and I don't know how to solve it, and I couldn't find the solution anywhere.

number_of_students = 0
students_that_passed = 0
amount = [0,0,0,0,0,0]
passing_percentage = 0
sum = 0
points_average = 0
contor = 6
star = "*"
 
while True:
    x, y = input("Exam points and exercises completed: ").split()
    if x=="" and y=="":
        print("Statistics:")
        break
    else:
        x=int(x)
        y=int(y)
        x=x+y%10
        sum +=x
        number_of_students+=1
        if x>=0 and x <=14:
            amount[0]+=1
        if x>=15 and x<=17:
            amount[1]+=1
            students_that_passed+=1
        if x>=18 and x<=20:
            amount[2]+=1
            students_that_passed+=1
        if x>=21 and x<=23:
            amount[3]+=1
            students_that_passed+=1
        if x>=24 and x<=27:
            amount[4]+=1
            students_that_passed+=1
        if x>=28 and x<=30:
            amount[5]+=1
            students_that_passed+=1
 
    points_average = sum/number_of_students
    passing_percentage = (students_that_passed/number_of_students)*100
 
print(f"Points average: {points_average}")
print(f"Pass percentage: {passing_percentage}")
print("Grade distribution:")
 
while contor>1:
    print(f"  {contor-1}:{amount[contor-1]*star}")
 
  • Welcome to Stack Overflow. In your own words, what do you think should be the result of the `.split` call on the input, if the user does not type anything? How many items do you expect to be in such a list, and why? What do you expect to happen, when this list is assigned to `x` and `y`? For future questions, please read [mre], and do not post irrelevant code - you [are expected](https://meta.stackoverflow.com/questions/261592) to identify *where* the problem is happening, first (hint: *read* the error message!), and show an example that *directly* causes the problem. – Karl Knechtel Aug 15 '23 at 15:49
  • Thinking about the line `if x=="" and y=="":`. What input string do you think would result in `["", ""]` when split? – slothrop Aug 15 '23 at 15:50
  • Please [edit] your question to include the error traceback. It should tell you exactly where in the code it's running into errors. That said, the only place I see you trying to unpack anything is here: `x, y = input("Exam points and exercises completed: ").split()` - if the result of `split` doesn't contain 2 values, you'll get this error. – JRiggles Aug 15 '23 at 15:50
  • "and I don't know how to solve it, and I couldn't find the solution anywhere." - when I copy and paste your question title [into a search engine](https://duckduckgo.com/?q=ValueError%3A+not+enough+values+to+unpack+%28expected+2%2C+got+0%29) I see lots of relevant results. Did you try this? What results did you find? What conclusions did you draw after reading them? Why were they not helpful? – Karl Knechtel Aug 15 '23 at 15:52
  • It's my first time asking a question here and I wrote in a hurry, and that's why I forgot to include the full error code. I did search but I didn't know how to fix my issue with those solutions, because I have been learning Python for not even a month and this is pretty difficult for me. I thought that by leaving the values of x and y empty in the input it would create a list with two empty elements in it – LucaR26 Aug 15 '23 at 16:14
  • Right, the thing to understand here is that when you write for example `x, y = "".split()`, the right-hand side (`"".split()`) has no knowledge of what the left-hand side is (`x, y`). So it would have no way of knowing whether to return a list of 2 empty strings, or 3, or 1. (In fact it returns an empty list, so 0 elements.) – slothrop Aug 15 '23 at 16:19

1 Answers1

0

the result of the .split() method you call on the input is not guaranteed to fit into the two variables x and y. You should always take such corner cases into account when you are accepting user input. Imagine if the user entered "1 2 3". Then there would be 3 values in the result of the .split method and only two variables to store them in, which would cause another exception. In your case, The user might have entered whitespace or an empty string, resulting in 0 values as the result of the .split method and two variables to store them in, which triggered an exception.

Amir Kooshky
  • 49
  • 1
  • 6
  • I understand where I made the mistake now, but I don't really understand how I can fix it. I tried to write two inputs on the same line instead of splitting it (x, y = input("Exam points and exercises completed: "), input("Exam points and exercises completed: ") - like this), but this just created another error – LucaR26 Aug 15 '23 at 16:21
  • You could do for example `vals = input('blah').split()` and then check what `len(vals)` is. – slothrop Aug 15 '23 at 16:22