13

I am a python beginner . 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 summon the main() I got this ValueError :

ValueError: too many values to unpack (expected 2)

what is wrong with this code ?

Leon Young
  • 585
  • 2
  • 6
  • 14
DAVID
  • 133
  • 1
  • 1
  • 4
  • 3
    @musical_coder: Don't try to remove `()` from print functions unless you're absolutely sure the OP isn't using Python 3. – Wooble Sep 30 '13 at 15:51

5 Answers5

18
  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.
wim
  • 338,267
  • 99
  • 616
  • 750
12

You need to split on the comma:

score1,score2 = input ("Enter two scores separated by a comma:").split(",")

Note however that score1 and score2 will still be strings. You will need to convert them into numbers using either float or int (depending on what number type you want).

See an example:

>>> score1,score2 = input("Enter two scores separated by a comma:").split(",")
Enter two scores separated by a comma:1,2
>>> score1
'1'
>>> score1 = int(score1)
>>> score1
1
>>> score1 = float(score1)
>>> score1
1.0
>>>
4

The input arrives as a single string. But Python has a split personality when it comes to strings: it can treat them as a single string value, or as a list of characters. When you tried to assign it to score1,score2 it decided you wanted a list of characters. Evidently you typed more than two characters, so it said you had too many.

The other answers have perfectly good suggestions for doing what you really wanted so I won't repeat them here.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
0

If you use args and give less values on running the file it will show you this error.

**To rectify that give correct values **

from sys import argv

one, two, three,four,five = argv

c=input("Enter the coffee you need?: ")

print("File Name is ",one)

print("First you need ",two)

print("The you need",three)

print("Last you need",four)

print("Last but not least you need",five)

print(f"you need above mentioned items to make {c}.")


While running code give it like this: **python hm5.py milk coffeepowder sugar water**

 milk == two

 coffeepowder ==three

 sugar == four

 water == five 

 one == file name (you don't need to give it while running



My output:
Enter the coffee you need?: filter coffee

First you need  milk

The you need coffeepowder

Last you need sugar

Last but not least you need water

you need above mentioned items to make filter coffee.
Yog
  • 817
  • 1
  • 10
  • 19
0
>>>number1,number2 = input("enter numbers: ").split(",")
enter numbers: 1,2
>>> number1
'1'
>>> number2
'2'

Then you can convert into integers

>>> number1 = int(number1)
>>> number2 = int(number2)
>>> average = (number1+number2)/2.0
>>> average
1.5
  • 1
    Checkout the right answer, you will need to convert them as numbers afterwards. Furthermore, do not get what your answer is an addition to other answers? – BlueSheepToken Jan 29 '19 at 09:28