Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

My Python Temperature Converter is not working

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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())
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading