Question: Need help fixing a code in Python please! #Lab on Input Validation #Metric System Conversion Program #This program will allow the user to convert either
Need help fixing a code in Python please! #Lab on Input Validation #Metric System Conversion Program
#This program will allow the user to convert either Fahrenheit degrees to Celsius #or Feet to Meters. The user will select which one they wish to do.
#Written by: Betsy Jenaway #Date: December 5, 2016
def main(): #Declare variables UserSelect = str() #Display opening message OpenMessage() #Based on what user enters process conversion or end program while True: #Get the user's choice UserSelect = GetUserChoice() # If input is not one of C, M, E while UserSelect not in ["C", "M", "E"]: # Re-Prompt user for correct value UserSelect = input(" Sorry you did not enter a C, M or E Please try again: "); # Calling corresponding functions if UserSelect == "C": ConvertCelsius() elif UserSelect == "M": ConvertMeters() else: print("Thank you for using our program") return
def OpenMessage(): #This message displays at the beginning of the program print("Welcome to the Metric Conversion Program") print("This program will take a unit of measurement and convert") print(" it to degrees Celsius or Meters.") print("*********************************************************") print()
def GetUserChoice(): #This message displays the menu. It also grabs the users choice and validates it #Declare local variables choice = "nothing" flag = False # Displaying Prompt to user choice = input("Enter C to convert to Celsius, M to convert to Meters, or E to exit: ") choice = choice.upper() if choice: return choice
def ConvertCelsius(): """ Converts to Celsius """ # Reading temperature f=input(" Enter degrees Fahrenheit: ") # Iterate till user enters a valid data while not isFloat(f): f = input(' Entered value must be a number. Please try again: ') # Converting to float f=float(f) # Converting to Celsius c=(float)(5/9) * (f -32) # Printing results print(" Degrees Celsius = %.2f" % c) def isFloat(f): """ Helper function that tests for valid float value """ try: # Trying to convert to float value f = float(f) return True except ValueError: # Error Occurred return False def ConvertMeters(): """ Convert to Meters """ # Reading feet f=input(" Enter feet: ")
# Iterate till user enters a valid value while (not f) or (not f.isnumeric()) or (int(f) < 0): f = input(' You did not enter a positive integer. Please try again: ') # Converts to integer f = int(f) # Converting to meters m = 0.305 * f # Printing results print(" Meters = %.2f" % m) main() OUTPUT/ERROR MESSAGE: Welcome to the Metric Conversion Program This program will take a unit of measurement and convert it to degrees Celsius or Meters. ********************************************************* Enter C to convert to Celsius, M to convert to Meters, or E to exit: Traceback (most recent call last): File "main.py", line 112, in
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
