Need to convert my current code into functions. Instructions for assingment post
ID: 3916612 • Letter: N
Question
Need to convert my current code into functions. Instructions for assingment posted below
###############################
For this portion of the lab, you will reuse the program you wrote in Lab 4. Redesign this solution using functions. For this lab:
You will define a function named main().
You will get input in the main function and pass it to the following functions:
MilesToKm()
FahToCel()
GalToLit()
PoundsToKg()
InchesToCm().
Each function will require that you have a local variable to store the result of the calculation. This result will then be displayed using the print statement from within the function.
Part 1B: Code
Use the design you created in part A. Write a complete and syntactically correct Python program.
This is my code from lab4a
#This program will convert various mesaurements into the metric system.
print('Welcome to the converter program!')
#Conversion of miles to km
counter = 0
miles = float(input('William, how many miles do you want to convert to kilometers? '))
while miles < 0:
print("'ERROR - Negative Value [ENDING PROGRAM]")
miles = float(input("Enter the correct value for miles "))
counter+=1
if counter > 2:
break
if counter <= 2:
kilometers= miles * 1.6
print("The distance in kilometers is: ", kilometers)
else:
print("Exceeded error count")
#Conversion of F to C
counter1 = 0
fahrenheit = float(input('William, what degrees fahrenheit do you want to convert to celsius? '))
while fahrenheit < 0:
print("'ERROR - Negative Value [ENDING PROGRAM]")
fahrenheit = float(input("Enter the correct value for fahrenheit "))
counter1+=1
if counter1 > 2:
break
if counter1 <= 2:
celsius = (fahrenheit - 32) * 5/9
print("The degrees in celsius is: ", celsius)
else:
print("Exceeded error count")
#Conversion of Gallons to Liters
counter2 = 0
gallons = float(input('William, how many gallons do you want to convert to liters? '))
while gallons < 0:
print("'ERROR - Negative Value [ENDING PROGRAM]")
gallons = float(input("Enter the correct value for gallons "))
counter2+=1
if counter2 > 2:
break
if counter2 <= 2:
liters = gallons * 3.9
print("The amount in liters is: ", liters)
else:
print("Exceeded error count")
#Conversion of Pound to Kg
counter3 = 0
pounds = float(input('William, how many pounds do you want to convert to kilograms? '))
while pounds < 0:
print("'ERROR - Negative Value [ENDING PROGRAM]")
pounds = float(input("Enter the correct value for pounds "))
counter3+=1
if counter3 > 2:
break
if counter3 <= 2:
kilograms = pounds * 0.45
print("The weight in kilograms is: ", kilograms)
else:
print("Exceeded error count")
#Conversion of Inches to cm
counter4 = 0
inches = float(input('William, how many inches do you want to convert to centimeters? '))
while inches < 0:
print("'ERROR - Negative Value [ENDING PROGRAM]")
inches = float(input("Enter the correct value for miles "))
counter4+=1
if counter4 > 2:
break
if counter4 <= 2:
centimeters = inches * 2.54
print("The distance in centimeters is: ", centimeters)
else:
print("Exceeded error count")
Explanation / Answer
def main():
miles = float(input('William, how many miles do you want to convert to kilometers? '))
MilesToKm(miles)
fahrenheit = float(input('William, what degrees fahrenheit do you want to convert to celsius? '))
FahToCel(fahrenheit)
gallons = float(input('William, how many gallons do you want to convert to liters? '))
GalToLit(gallons)
pounds = float(input('William, how many pounds do you want to convert to kilograms? '))
PoundsToKg(pounds)
inches = float(input('William, how many inches do you want to convert to centimeters? '))
InchesToCm(inches)
def MilesToKm(miles):
counter = 0
while miles < 0:
print("'ERROR - Negative Value [ENDING PROGRAM]")
miles = float(input("Enter the correct value for miles "))
counter+=1
if counter > 2:
break
if counter <= 2:
kilometers= miles * 1.6
print("The distance in kilometers is: ", kilometers)
else:
print("Exceeded error count")
def FahToCel(fahrenheit):
counter1 = 0
while fahrenheit < 0:
print("'ERROR - Negative Value [ENDING PROGRAM]")
fahrenheit = float(input("Enter the correct value for fahrenheit "))
counter1+=1
if counter1 > 2:
break
if counter1 <= 2:
celsius = (fahrenheit - 32) * 5/9
print("The degrees in celsius is: ", celsius)
else:
print("Exceeded error count")
def GalToLit(gallons):
counter2 = 0
while gallons < 0:
print("'ERROR - Negative Value [ENDING PROGRAM]")
gallons = float(input("Enter the correct value for gallons "))
counter2+=1
if counter2 > 2:
break
if counter2 <= 2:
liters = gallons * 3.9
print("The amount in liters is: ", liters)
else:
print("Exceeded error count")
def PoundsToKg(pounds):
counter3 = 0
while pounds < 0:
print("'ERROR - Negative Value [ENDING PROGRAM]")
pounds = float(input("Enter the correct value for pounds "))
counter3+=1
if counter3 > 2:
break
if counter3 <= 2:
kilograms = pounds * 0.45
print("The weight in kilograms is: ", kilograms)
else:
print("Exceeded error count")
def InchesToCm(inches):
counter4 = 0
while inches < 0:
print("'ERROR - Negative Value [ENDING PROGRAM]")
inches = float(input("Enter the correct value for miles "))
counter4+=1
if counter4 > 2:
break
if counter4 <= 2:
centimeters = inches * 2.54
print("The distance in centimeters is: ", centimeters)
else:
print("Exceeded error count")
"if__name__== "__main__"
main()
#Pls check the identation as it might get jumbled on copying from here. All the best
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.