Question

For this portion of the lab, you will reuse the program you wrote before That means...

For this portion of the lab, you will reuse the program you wrote before
That means you will open the lab you wrote in the previous assignment and change it. You should NOT start from scratch. Also, all the requirements/prohibitions from the previous lab MUST also be included /omitted from this lab.
Redesign the solution in the following manner:

1. All the functions used are value-returning functions.

2. Put the functions in an external module and let the main program import the module.

Below I am showing the program I must reuse. How can I add the new requirements? Thanks.

#Create functions to convert the given metric to another metric and print values
def MilesToKm(Miles):
Km = Miles * 1.6
print(f"Miles: {format(Miles,'.2f')}, kilometers: {format(Km,'.2f')}")

def FahToCel(Fahrenheit):
Celsius = (Fahrenheit - 32) * 5 / 9
print(f"farehnheit: {format(Fahrenheit,'.2f')}, celsius: {format(Celsius,'.2f')}")

def GalToLit(Gallons):
Liters = Gallons * 3.9
print(f"gallons: {format(Gallons,'.2f')}, liters: {format(Liters,'.2f')}")

def PoundsToKg(Pounds):
Kg = Pounds * 3.45
print(f"pounds: {format(Pounds,'.2f')}, kilograms: {format(Kg,'.2f')}")

def InchesToCm(Inches):
Cm = Inches * 2.54
print(f"inches: {format(Inches,'.2f')}, centimeters: {format(Cm,'.2f')}")

#Create main function to give choice of conversion
def main():
string = '''
1: Convert miles to km
2: Convert fahrenheit to celsius
3: Convert gallons to liters
4: Convert pounds to kg
5: Convert inches to cm'''

metric_name = ('miles','fahrenheit','gallons','pounds','inches')
convert_name = ('km','celsius','liters','kg','cm')
print(string)
choice = int(input('Enter your choice: '))
count = 3
flag = True
while(flag):
input_value = float(input("Please enter how much {0} would you like to convert into {1}: ".format(metric_name[choice-1] , convert_name[choice-1])))
  
if choice ==2:
if input_value >= 1000:
count -= 1
print('Invalid input! You have',count,'chance(s) to enter a valid input')
else:
FahToCel(input_value)
flag = False
else:
if input_value < 0:
count -= 1
print('Invalid input! You have',count,'chance(s) to enter a valid input')
else:
if choice == 1:
MilesToKm(input_value)
if choice == 3:
GalToLit(input_value)
if choice == 4:
PoundsToKg(input_value)
if choice == 5:
InchesToCm(input_value)
flag = False
if count == 0:
flag = False

main()
  
  

0 0
Add a comment Improve this question Transcribed image text
Answer #1
  1. Create a file named conversions.py and implement your functions in that file.
  2. Import the functions in the main file like this "from conversions import * "
  3. We can use separate files for different functions also

Code

Conversions.py

def MilesToKm(Miles):
       Km = Miles * 1.6
       print(f"Miles: {format(Miles,'.2f')}, kilometers: {format(Km,'.2f')}")

def FahToCel(Fahrenheit):
       Celsius = (Fahrenheit - 32) * 5 / 9
       print(f"farehnheit: {format(Fahrenheit,'.2f')}, celsius: {format(Celsius,'.2f')}")

def GalToLit(Gallons):
       Liters = Gallons * 3.9
       print(f"gallons: {format(Gallons,'.2f')}, liters: {format(Liters,'.2f')}")

def PoundsToKg(Pounds):
       Kg = Pounds * 3.45
       print(f"pounds: {format(Pounds,'.2f')}, kilograms: {format(Kg,'.2f')}")

def InchesToCm(Inches):
       Cm = Inches * 2.54
       print(f"inches: {format(Inches,'.2f')}, centimeters: {format(Cm,'.2f')}")

  

Main.py

from conversions import *
#Create main function to give choice of conversion
def main():
   string = '''
   1: Convert miles to km
   2: Convert fahrenheit to celsius
   3: Convert gallons to liters
   4: Convert pounds to kg
   5: Convert inches to cm'''

   metric_name = ('miles','fahrenheit','gallons','pounds','inches')
   convert_name = ('km','celsius','liters','kg','cm')
   print(string)
   choice = int(input('Enter your choice: '))
   count = 3
   flag = True
   while(flag):
       input_value = float(input("Please enter how much {0} would you like to convert into {1}: ".format(metric_name[choice-1] , convert_name[choice-1])))

       if choice ==2:
           if input_value >= 1000:
               count -= 1
               print('Invalid input! You have',count,'chance(s) to enter a valid input')
           else:
               FahToCel(input_value)
               flag = False
       else:
           if input_value < 0:
               count -= 1
               print('Invalid input! You have',count,'chance(s) to enter a valid input')
           else:
               if choice == 1:
                   MilesToKm(input_value)

              if choice == 2:
                   FahToCel(input_value)
               if choice == 3:
                   GalToLit(input_value)
               if choice == 4:
                   PoundsToKg(input_value)
               if choice == 5:
                   InchesToCm(input_value)
           if count == 0:
               flag = False
main()

Output

def Miles Tokm(Miles): Km = Miles * 1.6 print(f"Miles: {format(Miles,'.2f')}, kilometers: {format(Km,'.2f')}"), def FahToCel(Fahrenheit): Celsius = (Fahrenheit - 32) * 5 / 9 print(F"farehnheit: {format(Fahrenheit,'.2f')}, celsius: {format(Celsius,'.2f')}"), def Gallolit(Gallons): Liters = Gallons * 3.9 print(f"gallons: {format(Gallons,'.2f')}, liters: {format(Liters,'.2f')}"), def PoundsTokg(Pounds): kg = Pounds * 3.45 print(f"pounds: {format(Pounds,'.2f')}, kilograms: {format(Kg,'.2f')}"), def pounding the one and Format (pounds, ".2f"}}," def Inches ToCm(Inches): Cm = Inches * 2.54 print(f"inches: {format(Inches,'.2f')}, centimeters: {format(Cm,'.2f')}"),

from conversions import * #Create main function to give choice of conversion def main(): string = 1: Convert miles to km 2: Convert fahrenheit to celsius 3: Convert gallons to liters 4: Convert pounds to kg 5: Convert inches to cm metric name= ('miles', 'fahrenheit', 'gallons', 'pounds", "inches'), convert_name = ('km', 'celsius', 'liters', 'kg, 'cm') print(string) choice = int(input('Enter your choice: ") count = 3 flag = True while(flag): input value = float(input("Please enter how much {@} would you like to convert into {1}: ".format(metric_name[ choice-1] , convert_name[ choice-1]))) if choice ==2: if input_value >= 1000: count -= 1 print('Invalid input! You have',count, 'chance(s) to enter a valid input') else: FahToCel(input_value) flag = False else: input_value < 0: count -= 1 print("Invalid input! You have',count, 'chance(s) to enter a valid input') else: if choice == 1: Milestokm(input_value) if choice == 3: GalToLit(input_value) if choice == 4: PoundsTokg(input_value) if choice == 5: InchesToCm(input_value) if count == 0: flag = False main()

C:\Users\Sai\Desktop\HomeworkLib>python main.py 1: Convert miles to km 2: Convert fahrenheit to celsius 3: Convert gallons to liters 4: Convert pounds to kg 5: Convert inches to cm Enter your choice: 1 Please enter how much miles would you like to convert into km: 1 Miles: 1.00, kilometers: 1.60 Please enter how much miles would you like to convert into km: 2.7 Miles: 2.70, kilometers: 4.32 Please enter how much miles would you like to convert into km: 8 Miles: 8.00, kilometers: 12.80 Please enter how much miles would you like to convert into km: -1 Invalid input! You have 2 chance(s) to enter a valid input Please enter how much miles would you like to convert into km: -1, Invalid input! You have 1 chance(s) to enter a valid input Please enter how much miles would you like to convert into km: -1 Invalid input! You have a chance(s) to enter a valid input

Add a comment
Know the answer?
Add Answer to:
For this portion of the lab, you will reuse the program you wrote before That means...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • #function to covert kilometer to miles #takes km input as an argument and return the calculation...

    #function to covert kilometer to miles #takes km input as an argument and return the calculation def kilometer_to_miles(km): return km * 0.6214 #function to covert miles to kilometer #takes miles input as an argument and return the calculation def miles_to_kilometer(miles): return miles/0.6214 #Main function to find the distance #by calling either of the two functions #prompt the user to enter his/her choice of distance conversion restart = 0 while (restart == 'y'): user_input = input ("Enter k to convert kilometer...

  • # convert.py # A program to convert Celsius temps to Fahrenheit # by: Susan Computewell def...

    # convert.py # A program to convert Celsius temps to Fahrenheit # by: Susan Computewell def main(): celsius = eval(input("What is the Celsius temperature? ")) fahrenheit = 9/5 * celsius + 32 print("The temperature is", fahrenheit, "degrees Fahrenheit.") main() PORTION ONE: Based the convert.py, so it can convert temperatures from Fahrenheit to Celsius, instead of original Celsius to Fahrenheit. Make sure to change the message to reflect the change of introduction and final result. Name it as: convert_v2.py. Run it...

  • Propose: In this lab, you will complete a Python program with one partner. This lab will...

    Propose: In this lab, you will complete a Python program with one partner. This lab will help you with the practice modularizing a Python program. Instructions (Ask for guidance if necessary): To speed up the process, follow these steps. Download the ###_###lastnamelab4.py onto your desktop (use your class codes for ### and your last name) Launch Pyscripter and open the .py file from within Pyscripter. The code is already included in a form without any functions. Look it over carefully....

  • Write a program in C++ that gives the temperature of earth at a depth. It must...

    Write a program in C++ that gives the temperature of earth at a depth. It must be in C++ and follow the information below: Problem description Write a program to take a depth in kilometers inside the earth as input data; calculate and display the temperature at this depth in both degrees Celsius and degree Fahrenheit. The formulas are: Celsius temperature at depth in km: celsius = 10 x depth(km) + 20 Convert celsius to fahrenheit: fahrenheit = 1.8 x...

  • Python 3 Question: All I need is for someone to edit the program with comments that...

    Python 3 Question: All I need is for someone to edit the program with comments that explains what the program is doing (for the entire program) def main(): developerInfo() #i left this part out retail = Retail_Item() test = Cash_Register() test.data(retail.get_item_number(), retail.get_description(), retail.get_unit_in_inventory(), retail.get_price()) answer = 'n' while answer == 'n' or answer == 'N': test.Menu() print() print() Number = int(input("Enter the menu number of the item " "you would like to purchase: ")) if Number == 8: test.show_items() else:...

  • Write a program in C that will convert all 12 months' average temperature from Celsius to...

    Write a program in C that will convert all 12 months' average temperature from Celsius to Fahrenheit. You need to create an array named aye jump and prompt the user to input average temperature in Celsius for all 12 months. After that, you need to develop a user temperatures from Celsius to Fahrenheit and pass the array as reference and convert all temperatures from Celsius to Fahrenheit. Next, compute the average temperature of the year and assign it to a...

  • Write a single C++ program that performs the following conversion (The modulus divide will help you...

    Write a single C++ program that performs the following conversion (The modulus divide will help you get the remainder as shown in class) 39 / 12 = 3 and 39 % 12 = 3, so 3 feet 3 inch(es) Height conversion 1 meter = 39 inches 12 inches = 1 foot Ask/Prompt the user to enter in the height in meters, convert and output the result in feet and inches Example 1 meters as input should produce 3 feet (foot)...

  • FOR PYTHON Write a Python program that can convert a Fahrenheit temperature to Celsius, or vice...

    FOR PYTHON Write a Python program that can convert a Fahrenheit temperature to Celsius, or vice versa. The program should use two custom functions, f_to_c and c_to_f, to perform the conversions. Both of these functions should be defined in a custom module named temps. Custom functionc_to_f should be a void function defined to take a Celsius temperature as a parameter. It should calculate and print the equivalent Fahrenheit temperature accurate to three decimal places. Custom function f_to_c should be a...

  • Write a Python program that can convert a Fahrenheit temperature to Celsius, or vice versa. The...

    Write a Python program that can convert a Fahrenheit temperature to Celsius, or vice versa. The program should use two custom functions, f_to_c and c_to_f, to perform the conversions. Both of these functions should be defined in a custom module named temps. Custom function c_to_f should be a void function defined to take a Celsius temperature as a parameter. It should calculate and print the equivalent Fahrenheit temperature accurate to three decimal places. Custom function f_to_c should be a value-returning...

  • You previously wrote a program that calculates and displays: Miles driven Miles per gallon Kilometers driven...

    You previously wrote a program that calculates and displays: Miles driven Miles per gallon Kilometers driven Liters of fuel consumed Kilometers per liter Messages commenting on a vehicle's fuel efficiency And also validates input Now we'll make a somewhat major change. We'll remove the code for obtaining input from a user and replace it with hardcoded data stored in lists. Because the data is hardcoded, the program will no longer need the code for validating input. So, you may remove...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT