×
Samples Blogs Make Payment About Us Reviews 4.9/5 Order Now

Conversion Function Assignment Solution using Python

July 09, 2024
Dr. Lauren Chen
Dr. Lauren
🇦🇺 Australia
Python
Dr. Lauren Chen, a seasoned expert with 7 years of experience, is a doctorate of Yale University. With an impressive track record of completing over 500 Python assignments, she possesses a profound understanding of complex programming concepts. Dr. Chen's dedication to excellence and her ability to simplify intricate topics make her an invaluable resource for students seeking guidance in Python programming.
Tip of the day
Use the REPL (Read-Eval-Print Loop) to test your code interactively, and practice writing concise, readable expressions to harness Scala’s powerful syntax effectively.
News
In 2024, universities abroad, like Dublin City University, have introduced cutting-edge programming courses, including Computer Engineering programs focusing on AI and machine learning.

The goal of this assignment is to give you some experience working with functions in Python. The program should display a menu with a list of conversions/calculations the program offers. The program should ask the user for a calculation choice, data required for the calculation, perform the calculation, and display the results. The program should allow the user to perform multiple calculations and exit when they are finished using the program. The program should offer at least 10 different conversions/calculations. Some of the calculations must require multiple inputs; you cannot have every calculation require a single input. You must create and use functions for every calculation/conversion and you must write a python assignment functions properly to accept inputs and return the result; your conversion/calculation functions should not get input from the user nor should they deliver the results to the screen.

The program should also create a text log file with all the calculations that were performed by the program. The log file should contain the date, time, operation, inputs, and results. Each line in the log file should represent one calculation that was performed. Make sure to label all values written to the log file, so that someone reading the log file would understand it.

Example Program:

*** Welcome to the CIS123 Conversion/Calculation Program ***

Conversions Menu:

  • Miles to Kilometers
  • Fahrenheit to Celsius
  • Calculate the volume of a cylinder
  • ..............
  • Exit Program

----------------------------------------------------------------------------

Enter a choice for the conversion to perform: 1

Enter the miles:100

Conversion results:160.9 kilometers

----------------------------------------------------------------------------

Enter a choice for the conversion to perform: 11

Thanks for using the conversion program. Goodbye for now.

Solution:

import math import datetime def MiltoKm(Mil): return 1.60934*Mil def KmtoMil(Km): return 0.621371*Km def FartoCel(Far): return (Far - 32)*5/9 def CeltoFar(Cel): return (Cel*9/5) + 32 def Kgtoton(Kg): return 0.00110231*Kg def tontoKg(ton): return ton*907.185 def RadtoDeg(rad): return rad*180/math.pi def DegtoRad(deg): return deg*math.pi/180 def VolofCylinder(r, h): return math.pi*r*r*h def AreaofCircle(r): return math.pi*r*r def main(): file = open("log.txt", "w") print("*** Welcome to the CIS123 Conversion/Calculation Program ***") print("Conversions Menu:") print("1.) Miles to Kilometers") print("2.) Kilometers to Miles") print("3.) Fahrenheit to Celsius") print("4.) Celsius to Fahrenheit") print("5.) Kilogram to ton") print("6.) ton to Kilogram") print("7.) Radians to Degree") print("8.) Degree to Radian") print("9.) Calculate volume of a cylinder") print("10.) Calculate area of a circle") print("11.) Exit Program") while True: print("-----------------------------------------------") usr_inp = input("Enter a choice for the conversion to perform: ") if usr_inp == "1": Mil = float(input("Enter the Miles: ")) Km = MiltoKm(Mil) print(f"Conversion Result: {Km} Kilometers") ts = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") file.write(f"{ts} Miles to Kilometers {Mil} Miles {Km} Kilometers\n") elif usr_inp == "2": Km = float(input("Enter the Kilometers: ")) Mil = KmtoMil(Km) print(f"Conversion Result: {Mil} Miles") ts = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") file.write(f"{ts} Kilometers to Miles {Km} Kilometers {Mil} Miles\n") elif usr_inp == "3": Far = float(input("Enter the Temperature in Farenheit: ")) Cel = FartoCel(Far) print(f"Conversion Result: {Cel} Celsius") ts = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") file.write(f"{ts} Farenheight to Celsius {Far} F {Cel} °C\n") elif usr_inp == "4": Cel = float(input("Enter the Temperature in Celsius: ")) Far = CeltoFar(Cel) print(f"Conversion Result: {Far} Farenheit") ts = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") file.write(f"{ts} Celsius to Farenheight {Cel} °C to {Far} F\n") elif usr_inp == "5": Kg = float(input("Enter the Kilograms: ")) ton = Kgtoton(Kg) print(f"Conversion Result: {ton} Ton") ts = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") file.write(f"{ts} Kilogram to ton {Kg} Kg to {ton} Ton\n") elif usr_inp == "6": ton = float(input("Enter the Tons: ")) Kg = tontoKg(ton) print(f"Conversion Result: {Kg} Kilograms") ts = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") file.write(f"{ts} Ton to Kilogram {ton} Ton to {Kg} Kg\n") elif usr_inp == "7": rad = float(input("Enter the Radians: ")) deg = RadtoDeg(rad) print(f"Conversion Result: {deg} Degrees") ts = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") file.write(f"{ts} Radians to Degree {rad} Rad to {deg} Deg\n") elif usr_inp == "8": deg = float(input("Enter the Degrees: ")) rad = DegtoRad(deg) print(f"Conversion Result: {rad} Radians") ts = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") file.write(f"{ts} Degree to Radian {deg} Deg to {rad} Rad\n") elif usr_inp == "9": r = float(input("Enter the radius of Cylinder in m: ")) h = float(input("Enter the height of Cylinder in m: ")) Vol = VolofCylinder(r,h) print(f"Conversion Result: {Vol} m^3") ts = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") file.write(f"{ts} radius {r} height {h} Volume of Cylinder {Vol} \n") elif usr_inp == "10": r = float(input("Enter the radius of Circle in m:")) area = AreaofCircle(r) print(f"Conversion Result: {area} m^2") ts = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") file.write(f"{ts} radius {r} Area of Circle {area} \n") elif usr_inp == "11": print("Thanks for using the conversion program. Good bye for now.") break file.close() main()

Related Samples

Explore our collection of free Python assignment samples to get valuable insights and examples. Whether you're learning Python or tackling assignments, these resources can help you understand concepts and enhance your programming skills. Access them now for free!