Question: Redesign the solution in the following manner: 1. Create a menu and ask the user which of the following conversions they wish to perform: a.
Redesign the solution in the following manner:
1. Create a menu and ask the user which of the following conversions they wish to perform:
a. Miles to kilometers
b. Gallons to liters
c. Dollars to GBP
d. Pounds to kilograms
e. Inches to centimeters
f. Fahrenheit to Celsius
2. Your program must raise an exception if the user chooses any item not on the menu presented. Along with raising an exception, write the code to handle this exception. (LO 5)
3. Once the user has chosen a menu item the program should:
a. Ask the user for a value to convert. Refer to the input validations in Lab 3. Your program must raise and exception, and handle the exception, if an input errors occurs.
b. Perform the conversion and write the original value, the original unit, the converted value, and the converted unit to an output file named conversions.txt.
c. Repeat steps a and b 4 times (in a loop)
######################################################################################################################
def mile(): miles = int (input("Tell me how many miles you would like to convert: ")) # Error Check for Miles input count =0 if (miles < 0): for count in range (2): print ("Invalid input Miles cannot be negative, please Enter again.") count = count + 1 miles = float (input ('How many Miles? ')) print ("Miles Entered: ", miles) while (miles < 0 and count >= 2): print ("Invalid input Miles cannot be negative, Program will End.") quit() else: print ("Miles Entered: ", miles) conv_fac = 1.6 kilometers = miles * float(conv_fac) return (miles, kilometers)
################################################################## def fahrenheit(): fahrenheit = float (input ('Enter degree Fahrenheit: ')) # Error Check for Fahrenheit input count =0 if (fahrenheit > 1000): for count in range (2): print ("Invalid input Fahrenheit Degree cannot be above 1000 degrees, please Enter again.") count = count + 1 fahrenheit = float(input('Enter degree Fahrenheit: ')) print("Fahrenheit Degrees Entered: ", fahrenheit) while(fahrenheit > 1000 and count >= 2): print("Invalid input Fahrenheit cannot be negative, Program will End.") quit() else: print("Fahrenheit Degrees Entered: ", fahrenheit) conv_fac1 = (fahrenheit -32) * 5/9 celsius = conv_fac1 return (fahrenheit,celsius)
################################################################## def gallons(): gallons = float(input('How Many Gallons: ')) # Error Check for Gallons input count =0 if(gallons < 0): for count in range (2): print("Invalid input Gallons cannot be negative, please Enter again.") count = count + 1 gallons = float(input('How Many Gallons: ')) print("Gallons Entered: ", gallons) while(gallons < 0 and count >= 2): print("Invalid input Gallons cannot be negative, Program will End.") quit() else: print("Gallons Entered: ", gallons) conv_fac2 = 3.9 liters = gallons * conv_fac2 return (gallons,liters)
################################################################## def pounds(): pounds = float(input('How Many Pounds: ')) # Error Check for Pounds input count =0 if(pounds < 0): for count in range (2): print("Invalid input Pounds cannot be negative, please Enter again.") count = count + 1 pounds = float(input('How Many Pounds: ')) print("Pounds Entered: ", pounds) while(Pounds < 0 and count >= 2): print("Invalid input Pounds cannot be negative, Program will End.") quit() else: print("Pounds Entered: ", pounds) conv_fac3 = 0.45 kilograms = pounds * conv_fac3 return (pounds,kilograms) ################################################################## def inches(): inches = float(input('How Many Inches: ')) # Error Check for Inches input count =0 if (inches < 0): for count in range (2): print ("Invalid input Inches cannot be negative, please Enter again.") count = count + 1 inches = float(input('How Many Inches: ')) print ("Inches Entered: ", inches) while (inches < 0 and count >= 2): print ("Invalid input Inches cannot be negative, Program will End.") quit () else: print ("Inches Entered: ", inches) conv_fac4 = 2.54 centimeters = inches * conv_fac4 return (inches,centimeters) ################################################################## def dollars(): Dollars = float (input ('How Many Dollars: ')) #Error Check for Dollars input count =0 if (Dollars < 0): for count in range (2): print ("Invalid input Dollars cannot be negative, please Enter again.") count = count + 1 Dollars = float (input ('How Many Dollars: ')) print ("Dollars Entered: ", Dollars) while (Dollars < 0 and count >= 2): print ("Invalid input Dollars cannot be negative, Program will End.") quit () else: print ("Dollars Entered: ", Dollars) conv_fac5 = 0.69 GBP = Dollars * conv_fac5 return (Dollars,GBP)
################################################################## def display(miles , kilometers, fahrenheit, celsius, gallons,liters,pounds,kilograms,inches,centimeters,Dollars,GBP): print('%0.3f Miles is equal to %0.3f Kilometers' %(miles,kilometers)) print('%0.1f degree Fahrenheit is equal to %0.1f degree Celsius' %(fahrenheit,celsius)) print('%0.3f Gallons is equal to %0.3f Liters' %(gallons,liters)) print('%0.3f Pounds is equal to %0.3f kilograms' %(pounds,kilograms)) print('%0.3f Inches is equal to %0.3f Centimeters' %(inches,centimeters)) print('%0.3f Dollars is equal to %0.3f GBP (British Pounds)' %(Dollars,GBP)) ##################################################################
def main(): print("Hello this program will allow you to make conversions!") print("You will be able to convert Miles, Temperature, Gallons,") print("pounds, inches and US Dollars as well!")
ml,km = mile() fd, cel = fahrenheit() ga, lit = gallons() lb, kg = pounds() ic, cm = inches () da, gbp = dollars() dis = display(ml,km,fd,cel,ga,lit,lb,kg,ic,cm,da,gbp)
main()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
