How to use a for loop with input function

32,995

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))
Share:
32,995

Related videos on Youtube

userDepth
Author by

userDepth

Updated on August 19, 2020

Comments

  • userDepth
    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
      Dimitris Fasarakis Hilliard over 7 years
      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?
    • userDepth
      userDepth over 7 years
      Yep, just needed to cast an INT
    • Matthias
      Matthias over 7 years
      SemicolonOverflow Error
  • OneCricketeer
    OneCricketeer over 7 years
    Why not optimize your code to use Python's built in sum function?
  • MichaelMMeskhi
    MichaelMMeskhi over 7 years
    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.
  • MichaelMMeskhi
    MichaelMMeskhi over 7 years
    My bad I have made a mistake :)
  • OneCricketeer
    OneCricketeer over 7 years
    Your loop has for x in l. I fixed it
  • MichaelMMeskhi
    MichaelMMeskhi over 7 years
    Thank you. Silly mistakes kill me :\
  • OneCricketeer
    OneCricketeer over 7 years
    The parameter name needs not match the variable. My edit worked. Yours was meaningless
  • MichaelMMeskhi
    MichaelMMeskhi over 7 years
    True as well but for the sake of simplicity