Question

How to use strings as requests for numerical input in python

Posted March 18, 2021 177 views
PythonDevelopment

I need to ask user question in Python 3. User will input float number.
What data type,or combination should I use to then use that number in math operation deeper in program?

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
1 answer

Hello @huntz4k ,

There is no such method, that can be used to take input as a float directly – but input string can be converted into a float by using float() function which accepts a string or a number and returns a float value. Here is a block of code which converts the user input to float(), prints the number and its data type.

# reading a value, converting to float
# printing value and it's type
val = float(input("Enter any number: "))
print("value of val2: ", val)
print("type of val2: ", type(val))

Please check this tutorial to do math operations
https://www.digitalocean.com/community/tutorials/how-to-do-math-in-python-3-with-operators

Hope this helps!

Cheers,
Lalitha

by Lisa Tagliaferri
This tutorial will go over operators that can be used with number data types in Python.