Here is my program:


#avg2.py
# A simple program to average two exam scores
# Illustrates use of multiple input

def main():
print('This is a program to average two exam scores')

score1, score2 = float(input('Enter two scores separated by a comma: '))
average = (score1 + score2) / 2.0

print('The average of the scores is:', average)

main()


When I run the program and enter two values, e.g.:
This is a program to average two exam scores
Enter two scores separated by a comma: 2, 3


I get the following error:
line 8, in main
score1, score2 = float(input('Enter two scores separated by a comma: '))
ValueError: invalid literal for float(): 2, 3

How do I fix this? :?:

Member Avatar for masterofpuppets

hi,
try to remove the float function from the input, i.e

def main():
    print('This is a program to average two exam scores')

    score1, score2 = input( 'Enter two scores separated by a comma: ' )
    average = ( score1 + score2 ) / 2.0

    print('The average of the scores is:', average)

main()

There's no need for the float conversion here because you divide by a float '2.0' and this is automatically considered a float division :)

Thanks. Unfortunately I get another error when I remove the float function:

line 8, in main
score1, score2 = input('Enter two scores separated by a comma: ')
ValueError: too many values to unpack

Thanks. Unfortunately I get another error when I remove the float function:

line 8, in main
score1, score2 = input('Enter two scores separated by a comma: ')
ValueError: too many values to unpack

So this is what it looks right now:

def main():
    print('This is a program to average two exam scores')

    score1, score2 = input('Enter two scores separated by a comma: ')
    average = (score1 + score2) / 2.0

    print('The average of the scores is:', average)

main()

:(

Member Avatar for masterofpuppets

hi
what version of python are you using, because i noticed your print statement is different. Maybe that's the reason. I tried it on python 2.6 and it seems to work :)

I'm using python 3.1. and I want it to work with the new update. But i dont know how to do it on python 3.1. Thats the problem :/

Think input(python 3) can not take 2 values.
You can do it this way.

>>> score1 = int(input('Enter first number: '))
Enter first number: 5
>>> score2 = int(input('Enter second number: '))
Enter second number: 8
>>> average = (score1 + score2) / 2.0
>>> average
6.5
>>>

Indeed

Python 3.1.1 (r311:74480, Sep  8 2009, 15:48:28) 
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print(repr(input("enter your input: ")))
enter your input: 4, 5  
'4, 5'

So the input returned a single string.

Or you can process the input string ...

def main():
    print('This is a program to average two exam scores')

    # Python3 returns a string
    s = input('Enter two scores separated by a comma: ')
    score1, score2 = s.split(',')
    average = (float(score1) + float(score2)) / 2.0

    print('The average of the scores is:', average)

main()
Member Avatar for masterofpuppets

Or you can process the input string ...

def main():
    print('This is a program to average two exam scores')

    # Python3 returns a string
    s = input('Enter two scores separated by a comma: ')
    score1, score2 = s.split(',')
    average = (float(score1) + float(score2)) / 2.0

    print('The average of the scores is:', average)

main()

Yeah that's a great alternative :)

Yet, i'm still just a padawan and is in the process of learning to master 'the google'.

Thanks for the answers!

hi
what version of python are you using, because i noticed your print statement is different. Maybe that's the reason. I tried it on python 2.6 and it seems to work :)

The reason this is happening is that "input" in python 3 works the same as "raw_input" in python 2.x, which takes the input and puts it into a string. In python 2.x the "input" function actually treats the user's input as literal python commands. So when you input "2, 3" to "input" it sees it not as a string but as you defining a tuple, which is why the unpacking works.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.