Question

How to use strings as requests for numerical input in python

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?


Submit an answer

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!

Sign In or Sign Up to Answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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