How to use a for loop with input function
Solution 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))
Solution 2
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 :)
Solution 3
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))
Related videos on Youtube
userDepth
Updated on August 19, 2020Comments
-
userDepth over 3 years
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'
-
Dimitris Fasarakis Hilliard over 7 yearsThink of the type of the value returned by
input(..)
by tryingtype(input("> "))
in your console; you'll see it is of typestr
. Can astr
be added to a float (sums
) without first being converted? -
userDepth over 7 yearsYep, just needed to cast an INT
-
Matthias over 7 years
SemicolonOverflow Error
-
-
OneCricketeer over 7 yearsWhy not optimize your code to use Python's built in sum function?
-
MichaelMMeskhi over 7 yearsYou 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.
-
MichaelMMeskhi over 7 yearsMy bad I have made a mistake :)
-
OneCricketeer over 7 yearsYour loop has
for x in l
. I fixed it -
MichaelMMeskhi over 7 yearsThank you. Silly mistakes kill me :\
-
OneCricketeer over 7 yearsThe parameter name needs not match the variable. My edit worked. Yours was meaningless
-
MichaelMMeskhi over 7 yearsTrue as well but for the sake of simplicity