Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about **How to use a for loop with input function** **in Python**. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

## How to use a for loop with input function?

**How to use a for loop with input function?**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.**use a for loop with input function**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.

## Method 1

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 🙂

## Method 2

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))

**Conclusion**

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

**Also, Read**