Report this

What is the reason for this report?

How to use strings as requests for numerical input in python

Posted on March 18, 2021

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?



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

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.

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

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.