0

I'm new to coding and I'm trying to build a simple conversion between Celcius and Fahrenheit but I keep running into issues. If I run the program and type and hit enter nothing happens or else other errors. I'd really appreciate help and for you to explain as much as possible so I can learn.

#!/usr/bin/python3

def fahrenheitToCelsius(fahrenheit):
     celsius = (fahrenheit - 32.0) * (5.0/9.0)
     return celsius

def celsiusToFahrenheit(celsius):
   fahrenheit = (9.0/5.0) * celsius + 32.0
   return fahrenheit

print('Welcome')

userInput = 0

while userInput != 3 :
   userInput = input('''Main Menu 
   1:Fahrenheit to Celsius
   2:Celsius to Fahrenheit
   3:Exit program
   Please enter 1, 2 or 3:''')

if userInput == 1:
   fahren = input('\nPlease enter degrees Fahrenheit: ')

   try:
       fahren = float(fahren)
   except:
       print('Sorry, %s is not a valid number' % fahren)
       exit(1)
    
cels = fahrenheitToCelsius(fahren)

print('%s degrees Fahrenheit equals %d degrees Celsius' % fahren % cels)

elif userInput == 2:
    cels = input('\nPlease enter degrees Celsius: ')

    try:
        cels = float(cels)
    except:
        print('\nSorry, %s is not a valid number' % cels)
        exit(1)

    fahren = celsiusToFahrenheit(cels)

    else:
        print('Invalid entry')

2 Answers2

0

Python does not automatically convert values received by input() to their respective type. (Python 2 did this, so the confusion may originate from there). So, the number you receive will still be an str. "3" is never equal to 3. The easiest way is simply converting the input to a number by wrapping it in the int function:

userInput = int(input('...'))

You should wrap it in try-except the same way you did with inputting the temperature - but be sure to add a continue statement. (All it does is to go to the beginning of the loop, effectively "retrying" input).

try:
    userInput = int(input('...'))
except:
    print('Invalid number!')
    continue

A small thing you forgot is to indent the statements, beginning with if userInput == 1. This way, they would be executed outside the loop.

CoderCharmander
  • 1,862
  • 10
  • 18
0

Your indentation is a mess, you need to get this right in Python for your code to run -- it's not optional. Also:

  • userInput is input as a str but tested as an int. For the purpose at hand, either is valid but you have to be consistent.

  • A user input of '3' causes a 'Invalid entry' error message even though it's a valid option to leave the program.

  • User option '2' never outputs its results before returning to the main menu.

  • You should avoid a bare except: and instead specify the error (ValueError) you're trying to catch.

  • This isn't valid syntax print('%s ... %d ...' % fahren % cels) for the % operator and since you tagged this python-3.x, you might want to use the format() function instead.

Below is my rework of your code addressing the above and other issues:

import sys

def fahrenheitToCelsius(fahrenheit):
    return (fahrenheit - 32) * 5/9

def celsiusToFahrenheit(celsius):
    return 9/5 * celsius + 32

print("Welcome")

while True:
    userInput = int(input('''\nMain Menu
   1: Fahrenheit to Celsius
   2: Celsius to Fahrenheit
   3: Exit program
   Please enter 1, 2 or 3: '''))

    if userInput == 1:
        fahrenheit = input("\nPlease enter degrees Fahrenheit: ")

        try:
            fahrenheit = float(fahrenheit)
        except ValueError:
            sys.exit("Sorry, {} is not a valid number".format(fahrenheit))

        celsius = fahrenheitToCelsius(fahrenheit)

        print("{} degrees Fahrenheit equals {} degrees Celsius".format(fahrenheit, celsius))

    elif userInput == 2:
        celsius = input("\nPlease enter degrees Celsius: ")

        try:
            celsius = float(celsius)
        except ValueError:
            sys.exit("Sorry, {} is not a valid number".format(celsius))

        fahrenheit = celsiusToFahrenheit(celsius)

        print("{} degrees Celsius equals {} degrees Fahrenheit".format(celsius, fahrenheit))

    elif userInput == 3:
        break

    else:
        print("Invalid entry")
cdlane
  • 40,441
  • 5
  • 32
  • 81