Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need in Python Please Help! 1. Create a menu and ask the user which of the follo

ID: 3687566 • Letter: N

Question

Need in Python

Please Help!

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.
3. Once the user has chosen a menu item the program should:
a. Ask the user for a value to convert.
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).


# Using Functions
# converts Miles into kilometers
# converts temperature in Fahrenheit to Celsius
# converts Gallons to liters
# converts Pounds to kilograms
# converts Inches to centimeters
# converts Dollars to GBP
# takes input from the user and Error checking
##################################################################
##################################################################
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()

Explanation / Answer

"""

This converts between the following things.

Temperature:    Fahrenheit (F), Celsius (C), Kelvin (K)

Currency:       US Dollars ($), Pounds (£), Euros (€), Shekel (), Yen (¥)

Volume:         Kiloliters (kL), liters (L), milliliters (mL),
                teaspoons (tsp), tablespoons (tbsp), fluid ounces (floz),
                cups (c), pints (pt), quarts (qt), gallons (gal)

Mass:           Tonnes (t), kilograms (kg), grams (g), milligrams (mg)

Length:         Kilometers (km), meters (m), centimeters (cm),
                millimeters (mm), miles (mi), yards (y), feet (ft),
                inches (in)

Time:           Years (y), months (mth), days (d), hours (hr),
                minutes (min), seconds (sec)
"""

# Calls the correct conversion method based on the number the user chooses
def chooseType():

    print("Choose the type of measurement you would like to use.")
    print("1. Temperature")
    print("2. Currency")
    print("3. Volume")
    print("4. Mass")
    print("5. Length")
    print("6: Time")
    choice = int(input("Enter a number: "))

    if choice == 1:
        tempConverter()
    elif choice == 2:
        moneyConverter()
    elif choice == 3:
        volConverter()
    elif choice == 4:
        massConverter()
    elif choice == 5:
        lengthConverter()
    elif choice == 6:
        timeConverter()
    else:
        print("Not a valid choice.")
        chooseType()


# A general converter. Prints out the available units, then takes input to figure out what to convert to and from.
# For this to work, the the ratios of each element of unitArray to the first element of unitArray must be in the
# same position in conversionArray as the unit who the ratio pertains to is in unitArray.
# Example:
#   unitArray = ["meters", "kilometers", "centimeters"]
#   conversionArray = [1, 1000, .01]
# So, centimeters * conversionrate[2] = centimetersInMeters.
def converter(unitString, unitArray, conversionArray):

    print("You can convert between any of the following units:")
    print(unitString)
    start = input("Enter the abbreviation of the units you're starting with: ")
    startNum = float(input("Enter the number of units to start with: "))
    end = input("Enter the abbreviation of the units you'd like to convert to: ")

    if start.upper() in unitArray and end.upper() in unitArray:
        # Converts to a standard unit, always the first one in unitArray
        endNum = startNum * conversionArray[unitArray.index(start.upper())]
        # Converts from the standard unit to the final unit
        endNum *= (1 / conversionArray[unitArray.index(end.upper())])
        print("{0} {1} in {2} is {3}.".format(startNum, start, end, endNum))

    else:
        print("One or both of your units were not valid.")


# Converts temperatures. Hardcoded because the converter method doesn't work with temperatures
# since they're not a fixed ratio.
def tempConverter():

    tempString = "Fahrenheit (F), Celsius (C), Kelvin (K)"
    tempArray = ["F", "C", "K"]
    print("You can convert between any of the following units: ")
    print(tempString)
    start = input("Enter the abbreviation of the units you're starting with: ")
    startNum = float(input("Enter the number of units to start with: "))
    end = input("Enter the abbreviation of the units you'd like to convert to: ")

    if start.upper() in tempArray and end.upper() in tempArray:

        if start.upper() == "f" and end.upper() == "c":
            endNum = (startNum - 32) * (5/9)
        elif start.upper() == "f" and end.upper() == "k":
            endNum = (5/9) * (startNum - 32) + 273
        elif start.upper() == "c" and end.upper() == "f":
            endNum = (9/5) * startNum + 32
        elif start.upper() == "c" and end.upper() == "k":
            endNum = startNum + 273
        elif start.upper() == "k" and end.upper() == "f":
            endNum = (startNum - 273) * (9/5) + 32
        elif start.upper() == "k" and end.upper() == "c":
            endNum = startNum - 273
    else:
        print("You did not enter valid units")
        return

    print("{0} degrees {1} is {2} degrees {3}.".format(startNum, start, ("%.2f" % endNum), end))


def moneyConverter():

    moneyString = "US Dollars (USD), Pounds (GBP), Euros (EUR), Shekel (ILS), Yen (JPY)"
    moneyArray = ["USD", "GBP", "EUR", "ILS", "JPY"]
    moneyConversionArray = [1, 1.56, 1.12, .26, .0084]
    converter(moneyString, moneyArray, moneyConversionArray)


def volConverter():

    volString = "Kiloliters (kL), liters (L), milliliters (mL), teaspoons (tsp), tablespoons (tbsp), fluid ounces (floz), cups (c), pints (pts), quarts (qts), gallons (gals)"
    volArray = ["L", "KL", "ML", "TSP", "TBSP", "FLOZ", "C", "PTS", "QTS", "GALS"]
    volConversionArray = [1, 1000, .001, .0049, .0148, .2366, .4732, .9464, 3.7854]
    converter(volString, volArray, volConversionArray)


def massConverter():

    volString = "Tonnes (t), kilograms (kg), grams (g), milligrams (mg)"
    volArray = ["G", "T", "KG", "MG"]
    volConversionArray = [1, 1000000, 1000, .001]
    converter(volString, volArray, volConversionArray)


def lengthConverter():

    volString = "Kilometers (km), meters (m), centimeters (cm), millimeters (mm), miles (mi), yards (yds), feet (ft), inches (in)"
    volArray = ["M", "KM", "CM", "MM", "MI", "YDS", "FT", "IN"]
    volConversionArray = [1, 1000, .01, .001, 1609.34, .9144, .3048, .0254]
    converter(volString, volArray, volConversionArray)


def timeConverter():

    volString = "Years (yrs), months (mths), days (d), hours (hrs), minutes (mins), seconds (secs)"
    volArray = ["HRS", "YRS", "MTHS", "D", "MINS", "SECS"]
    volConversionArray = [1, 8766, 730, 24, .0167, .000278]
    converter(volString, volArray, volConversionArray)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote