In this post, we are going to learn How to take multiple float inputs from User Python. In python 3. x input() function is used to take input from the user.But in python 2.x instead of input() we have to use raw_input() function.We have discussed all examples of python 3
How to take multiple float inputs from User Python
- Python take a float value input from User
- Python take multiple float input in one-line
- Python takes multiple float input with space
- Python how to take multiple float input using while loop
- Python how to take list of float input
1. Python take a float value input from User
In this python program, we will learn Python take input a float value from user.In python there no direct way to get float input from user.We simply convert input string to float by using the float() function.Let understand how do we input a float value in python
- The input() function to prompt user for input
- The float() function to convert input value to float
float_input = float(input("Enter float number : "))
print('The input float value is :',float_input)
Output
Enter float number : 12.9
The input float value is : 12.9
2. Python takes multiple float input in one-line
In this example we will learn Python takes multiple float input in one-line by simply using the split() function along with map() function and converting it to list and this is how to takes multiple float input in one-line in python.
- The input() function to primpt for user input
- The split() function to split the value by space
- The float() function is to simply convert values into float.
- The map() function return a map object that is list after applying float() function on each enter value.
float_input_list = list(map(float, input("Enter mutiple float values:").split()))
print('you have entered:',float_input_list)
Output
Enter mutiple float values:12.5 3.5 6.7 8.9
you have entered: [12.5, 3.5, 6.7, 8.9]
3. Python takes multiple float input with space
In this python example we will learn Python takes multiple float input with space with the help of split() function along with map() function.
- The input() function to primpt for user input
- The split() function to split the value by space
- The float() function is to simply convert values into float.
- The map() function return a map object that is list after applying float() function on each enter value.
try:
a,b,c,d = list(map(float, input("Enter value for a, b,c and d:").split()))
print('you have entered:',a,b,c,d)
except ValueError:
print('Please enter four values!')
Output
Enter value for a, b,c and d:12.3 14.5 16.5 17.8
you have entered: 12.3 14.5 16.5 17.8
4. Python how to take multiple float input using while loop
In this python program example we will learn Python how to take multiple input using while loop. we are iterating through each entered value using for loop and converting each value to float with the of float() function. The entered value is storing in multiple input variables.
We are handling exception if user input value more 4 by using try/catch block.
while True:
try:
a, b, c,d = [float(item) for item in input('Enter value for a, b,c and d: ').split()]
print('you hav entered:',a,b,c,d)
except ValueError:
print('Please enter four values!')
else:
break
Output
Enter value for a, b,c and d: 13.5 3 6.7 4.5
you hav entered: 13.5 3.0 6.7 4.5
5. Python how to take list of float input
In this python example we will understand Python how to take list of float as input using while loop. we are iterating through each entered value using for loop and converting each value to float using float() function. The entered values are storing in list.
- The while loop true to take continues input in python.This how to take continous input in python.
- The split() function to split the value by space
- try/catch handle exception i
while True:
try:
list = [float(item) for item in input('Enter mutiple float values: ').split()]
print('you hav entered:',list)
except ValueError:
print('enter value seperated by space')
else:
break
Output
Enter value for a, b,c and d: 13.5 3 6.7 4.5
you hav entered: [13.5, 3.0, 6.7, 4.5]
Summary
In this post we have learned How to take multiple float inputs from User Python with examples.