Search code examples
pythoninput

"ValueError: too many values to unpack" when trying to read two numbers as input


I was trying to run this code:

def main():
    print("This program computes the average of two exam scores.")
    score1, score2 = input("Enter two scores separated by a comma:")
    average = (score1 + score2) / 2.0
    print("The average of the score is:", average)

When I call main() I get this ValueError:

ValueError: too many values to unpack (expected 2)

What is wrong with this code?


Solution

    1. You need to split the input you receive, because it arrives all in one string
    2. Then, you'll need to convert them to numbers, because the term score1 + score2 will do string addition otherwise and you will get an error.