I tried to make a Temperature Converter but it doesn’t work for some reason, here’s my code
def getinput():
print("Enter the temperature")
a = float(input())
def Converter():
print("Press 1 for Fahrenheit to Celsius\nPress 2 for Celsius to Fahrenheit")
x = int(input())
if x == 1:
a = getinput()
return (a - 32) * 5/9
if x == 2:
a = getinput()
return (a + 32) * 9/5
Converter()
>Solution :
there are two issues here, the first being that getinput() returns nothing which causes the error
TypeError: unsupported operand type(s) for -: 'NoneType' and 'int'
The second issue is that the code does not print the result, you can either print the result from within the function or call print on the function call like so
def getinput():
print("Enter the temperature")
a = float(input())
return a
def Converter():
print("Press 1 for Fahrenheit to Celsius\nPress 2 for Celsius to Fahrenheit")
x = int(input())
if x == 1:
a = getinput()
return (a - 32) * 5/9
if x == 2:
a = getinput()
return (a + 32) * 9/5
print(Converter())
example output:
Press 1 for Fahrenheit to Celsius
Press 2 for Celsius to Fahrenheit
1
Enter the temperature
23
-5.0
as a small point to note input() takes a message as an argument
def getinput():
return float(input("Enter the temperature: "))
def Converter():
x = int(input("Press 1 for Fahrenheit to Celsius\nPress 2 for Celsius to Fahrenheit\n"))
if x == 1:
a = getinput()
return (a - 32) * 5/9
if x == 2:
a = getinput()
return (a + 32) * 9/5
print(Converter())
now getinput() is fairly redundant
def Converter():
x = int(input("Press 1 for Fahrenheit to Celsius\nPress 2 for Celsius to Fahrenheit\n"))
if x == 1:
a = float(input("Enter the temperature: "))
return (a - 32) * 5/9
if x == 2:
a = float(input("Enter the temperature: "))
return (a + 32) * 9/5
print(Converter())