Question: Completion in Python Problem Description A good friend of yours is creating a web site to help elementary school children understand the metric system. Your

Completion in Python

Problem Description

A good friend of yours is creating a web site to help elementary school children understand the metric system. Your friend has asked you for some help with the code. The program will offer the user a menu so that they can convert any unit of measurement into its metric equivalent. The user will select what they want to convert (i.e. feet to meters, inches to centimeters, etc.) and then enter a unit of measurement. Depending upon which conversion they select, the program will perform the proper conversion calculations and display the converted value to the user. The program will then ask the user if they want to continue converting values. Your friend wants to start out simple, so for right now all you need to do is create a simple menu offering the user the chance to convert Fahrenheit to Celsius and feet to meters. Your job will be to write the code for both of these options. When the program starts, it will offer the user an option C for Fahrenheit to Celsius, M for Feet to Meters or E to exit. The program will only accept these values and return back to user a message when they dont. The program will also allow the user the opportunity to enter another value. Additionally the program will need to contain a function that will determine the Fahrenheit to Celsius conversion. You can use the following equation: (5/9) * (Fahrenheit - 32) This function needs to accept positive and negative floats from the user. If the user enters anything else, the program needs to return an error message and offer the user the chance to correct their entry. Finally, there needs to be a function to handle the conversion between Feet to Meters. You can use the following equation: 0.305 * foot Much like the Fahrenheit to Celsius conversation, this function also needs to take in a number. However, for this function that number needs to be a positive integer. If the user does not enter a positive integer, the program needs to display an error message and offer the opportunity to re-enter.

This lab has 4 parts. For each part you will be given a template and then have to complete the writing of the code. When your output compares to the output listed below, you are good to move on to the next part of the lab.

Part 1 Specific Instructions

Make sure you start with the template provided. You will need to write a function that will take in to it a user's choice either C, M or E. This function will validate this choice and return it back to where it was called. Testing There will be 2 sets of input data to test if the user entered C, M or E. You do not need to worry about entering in any data. The tester will do this for you.

Template 1

#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 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(self): flag = False

print() print("Here are your choices:") print(" C for Celsius") print(" M for Meters") print(" E for Exit") print() choice = str(input("Enter your selection: ")) choice = choice.upper() if choice: return choice def ConvertCelsius(): while True: f=input(" Enter the temperature in Fahrenheit: ")

if f: try: f=float(f) if isinstance(f,float): c=(float)(5/9) * (f -32) print(" The equivalent temperature in celsius is %.2f" % c) break except: print(' Entered value must be a number ') continue else: print(" Cannot be empty. Enter Temperature ") def ConvertMeters(): while True: f=input(" Enter the feet value: ")

if f: try: f=float(f) if f >0 : m=0.305 * f print(" The equivalent metre is %.2f" % m) break else: print('Feet value must be positive') except: print(' Entered value must be a number ') continue else: print(" Cannot be empty. Enter feet ")

Part 2 and Template

In this second part of the lab you will write a function that will take in a value entered by the user to represent Fahrenheit. This function will make sure this value is a float. Once this has been validated it will convert that Fahrenheit number to a Celsius value. Finally the function will return the Celsius number back to where it was called. As a reference, here is the equation you will use:

(5/9) * (Fahrenheit - 32)

Template 2

#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 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(userchoice): #This function takes in the user's choice, validates it and returns it back to where it was called. ##Add your code from part 1 here def ConvertCelsius(somenumber): #write the code to validate the user's entry to be a float #And write the code to convert Fahrenheit to Celsius #to test correctly you will need to round your celsius variable celsius = round(celsius,2)

Part 3 and Template

This is a continuation of part 2. In this section of the lab you will write the code to validate the number the user enters for Feet. This number needs to be a positive integer. Once the number has been validated you will convert that number to meters and return that back to where it was called. As a reference, here is the equation you will use:

0.305 * foot

Template

#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 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(userchoice): #This function takes in the user's choice, validates it and returns it back to where it was called. ##Add your code from part 1 here def ConvertCelsius(somenumber): #write the code to validate the user's entry to be a float #And write the code to convert Fahrenheit to Celsius #to test correctly you will need to round your celsius variable celsius = round(celsius,2) #Copy and paste your code for ConverCelsius here. def ConvertMeters(): #Write the code to validate feet to be a positive integer

Part 4 and Template

In this section you will create a main() function that will call the other functions. In main() you will need to create a loop that will allow the user to enter C, M or E. When the user enters E, the loop will stop. If the user enters C, main() needs to ask the user for degrees Fahrenheit, call the function to not only validate the number but also convert a good number to degrees Celsius. Finally main() will print out the degrees Celsius. If the user enters M, main() will ask the user to enter the number of feet they want to convert. The program will then call the ConvertMeters function to not only validate the number the user entered but also to convert it to Meters. Once that number is calculated and returned to main(), it will be printed.

Output

When run the first time your program should look like the following:

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: a Sorry you did not enter a C, M or E Please try again: C Enter degrees Fahrenheit: -10.5 Degrees Celsius = -23.61 Enter C to convert to Celsius, M to convert to Meters or E to exit: M Enter feet: a You did not enter a positive integer. Please try again: -2 You did not enter a positive integer. Please try again: 2.5 You did not enter a positive integer. Please try again: 15 Meters = 4.575 Enter C to convert to Celsius, M to convert to Meters or E to exit: E Thank you for using our program 

Template

def main(): #Declare variables UserSelect = str() #Display opening message OpenMessage() #Get the user's choice UserSelect = GetUserChoice() #Based on what user enters process conversion or end program while UserSelect == "C" or UserSelect == "M": if UserSelect == "C": ConvertCelsius() else: ConvertMeters() #Display menu and ask user to make a choice UserSelect = GetUserChoice()

print("Thank you for using our program") 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

print() print("Here are your choices:") print(" C for Celsius") print(" M for Meters") print(" E for Exit") print() choice = input("Enter your selection: ") choice = choice.upper() if choice: return choice

def ConvertCelsius(): while True: f=input(" Enter the temperature in Fahrenheit: ")

if f: try: f=float(f) if isinstance(f,float): c=(float)(5/9) * (f -32) print(" The equivalent temperature in celsius is %.2f" % c) break except: print(' Entered value must be a number ') continue else: print(" Cannot be empty. Enter Temperature ") def ConvertMeters(): while True: f=input(" Enter the feet value: ")

if f: try: f=float(f) if f >0 : m=0.305 * f print(" The equivalent metre is %.2f" % m) break else: print('Feet value must be positive') except: print(' Entered value must be a number ') continue else: print(" Cannot be empty. Enter feet ")

main()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!