To receive input from the user, use the built-in input() function.
name = input("Enter your name ")
print("Hello",name)
Think about the name variable here. What is its type? How would we check?
type(name)
The formula for converting Fahrenheit temperatures to Celsius is $$C = (F - 32)*\frac{5}{9}$$
We could make this an interactive program with code like this:
fahrenheit_temp = input("Enter the temperature in Fahrenheit ")
celsius_temp = (fahrenheit_temp-32)*(5/9)
print("That's",celsius_temp,"in Celsius")
Read the error message! It says we can't subract an int from a string. Even though we typed a number, it treated fahrenheit_temp as a string.
Unfortunately, that's just the way input() works.
Fortunately, there's another built-in function we can use to convert something to an integer: int()
val = "32"
print( type(val) )
print( type(int(val)) )
fahrenheit_temp_str = input("Enter the temperature in Fahrenheit ")
fahrenheit_temp_int = int(fahrenheit_temp_str)
celsius_temp = (fahrenheit_temp_int-32)*(5/9)
print("That's",celsius_temp,"in Celsius")
Or, you can do it all in one step like this
fahrenheit_temp = int( input("Enter the temperature in Fahrenheit ") )
celsius_temp = (fahrenheit_temp-32)*(5/9)
print("That's",celsius_temp,"in Celsius")
What would happen if the user typed a floating-point value like 72.5?
Convert to a floating-point type with float()
Convert to a string with str()
fahrenheit_temp = float( input("Enter the temperature in Fahrenheit ") )
celsius_temp = (fahrenheit_temp-32)*(5/9)
print("That's",celsius_temp,"in Celsius")
target_number = 42
user_input = int( input("Enter a number greater than ",target_number) ) #doesn't work
#above code corrected
target_number = 42
user_input = int( input("Enter a number greater than "+str(target_number)+" ") )