2

I simply have to make a sum of three numbers and calculate the average

import sys
sums=0.0
k=3
for w in range(k):
    sums = sums + input("Pleas input number " + str(w+1) + " ")
print("the media is " + str(sums/k) + " and the Sum is " + str(sums))

And the error :

Pleas input number 1 1
Traceback (most recent call last):
  File "/home/user/Python/sec001.py", line 5, in <module>
    sums = sums + input("Pleas input number " + str(w+1) + " ");
TypeError: unsupported operand type(s) for +: 'float' and 'str'
3
  • 2
    Think of the type of the value returned by input(..) by trying type(input("> ")) in your console; you'll see it is of type str. Can a str be added to a float (sums) without first being converted? Commented Nov 3, 2016 at 14:30
  • 1
    Yep, just needed to cast an INT Commented Nov 3, 2016 at 14:32
  • SemicolonOverflow Error Commented Nov 3, 2016 at 14:54

3 Answers 3

3

Why not do the simple version then optimize it?

def sum_list(l):
    sum = 0
    for x in l:
        sum += x
    return sum

l = list(map(int, input("Enter numbers separated by spaces:  ").split())) 
sum_list(l)

Your problem was that you were not casting your input from 'str' to 'int'. Remember, Python auto-initializes data types. Therefore, explicit casting is required. Correct me if I am wrong, but that's how I see it.

Hope I helped :)

Sign up to request clarification or add additional context in comments.

7 Comments

Why not optimize your code to use Python's built in sum function?
You are correct! Beg my pardon, but why have you edited the parameter list to 'l' instead of 'n'. If 'l' is to be used then all changes must be made.
My bad I have made a mistake :)
Your loop has for x in l. I fixed it
Thank you. Silly mistakes kill me :\
|
1

The input() function returns a string(str) and Python does not convert it to float/integer automatically. All you need to do is to convert it.

import sys;
sums=0.0;
k=3;
for w in range(k):
    sums = sums + float(input("Pleas input number " + str(w+1) + " "));
print("the media is " + str(sums/k) + " and the Sum is " + str(sums));

If you want to make it even better, you can use try/except to deal with invalid inputs. Also, import sys is not needed and you should avoid using semicolon.

sums=0.0
k=3
for w in range(k):
    try:
        sums = sums + float(input("Pleas input number " + str(w+1) + " "))
    except ValueError:
        print("Invalid Input")
print("the media is " + str(sums/k) + " and the Sum is " + str(sums))

Comments

1

input returns a string and you need to create an int or float from that. You also have to deal with the fact that users can't follow simple instructions. Finally, you need to get rid of those semicolons - they are dangerous and create a hostile work environment (at least when you bump into other python programmers...!)

import sys
sums=0.0
k=3
for w in range(k):
    while True:
        try:
            sums += float(input("Pleas input number " + str(w+1) + " "))
            break
        except ValueError:
            print("That was not a number")
print("the media is " + str(sums/k) + " and the Sum is " + str(sums))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.