TOPIC 7: Python User input
Completion requirements
4. Input Numbers
The input from the user is treated as a string. Even if, in the example above, you can input a number, the Python interpreter will still treat it as a string.
You can convert the input into a number with the float() function.
Example: To find the square root, the input has to be converted into a number
import math
x = input("Enter a number:")
# find the square root of the number:
y = math.sqrt(float(x))
print(f"The square root of {x} is {y}")
Sample run:
Enter a number:16
The square root of 16 is 4.0
Note: The line import math at the top is needed because the program uses math.sqrt(). Without it, Python will show an error.