[Python] How good is this code? How can i optimize it? Can i export it as an exe file? [closed]

Discussão em 'Python' iniciado por Stack, Abril 16, 2025.

  1. Stack

    Stack Membro Participativo

    For context, Im a begginner and im trying to learn python, ive been coding for about a month maybe, idk. I took up the personal project of creating an all in one calculator, temperature and unit converter. I worked pretty hard on it for a few days and got it working. Now im just looking for feedback from experienced devs on how good the code is and how I can optimize it (because currently it kinda looks like yanderedev code). Also, is it possible to have the .py file be an executable file for regular people to use? Thank you. Heres the code:

    def add (x,y):
    return x + y
    def subtract (x,y):
    return x - y
    def multiply (x,y):
    return x * y
    def divide (x, y):
    return x / y

    def mainCalc():
    int1 = input ("Enter first number: ")
    int1 = float(int1)
    int2 = input ("Enter second number: ")
    int2 = float(int2)

    print ("Mathematical operations:")
    print ("1. Addition")
    print ("2. Subtraction")
    print ("3. Multiplication")
    print ("4. Division")
    operation=input ("Choose operation (1/2/3/4): ")
    operation = int (operation)

    while True:
    if operation == 1:
    print (int1, "+", int2, "=", add (int1, int2))
    elif operation == 2:
    print (int1, "-", int2, "=", subtract (int1, int2))
    elif operation == 3:
    print (int1, "*", int2, "=", multiply (int1, int2))
    elif operation == 4:
    print (int1, "/", int2, "=", divide (int1, int2))

    NewCalculation= int(input ("Do you want to do another conversion (1) or go back to the main menu (2)? (Choose 1 or 2): "))
    if NewCalculation == 1:
    continue
    else:
    CalcApp()



    def KelvinConverter():
    while True:
    CtoK = int(input ("Do you want to convert Celsius to Kelvin (1) or Kelvin to Celsius (2)? Enter 1 or 2: "))
    if CtoK == 1:
    initCvalue= float(input ("Enter temperature in degrees Celsius: "))
    print ("Your temperature in Kelvin is: ")
    print (initCvalue + 273.15, "K")
    elif CtoK == 2:
    initKvalue = float(input("Enter temperature in Kelvin: "))
    print ("Your temperature in degrees celsius is: ")
    print (initKvalue - 273.15, "C")

    NewCalculation= int(input ("Do you want to do another conversion (1) or go back to the main menu (2)? (Choose 1 or 2): "))
    if NewCalculation == 1:
    continue
    else:
    CalcApp()

    def FConvert():
    while True:
    FtoC = int(input ("Do you want to convert Farenheit to Celsius (1) or Celsius to Farenheit (2)? Enter 1 or 2: "))
    if FtoC == 1:
    initCvalue2= float(input ("Enter temperature in degrees Farenheit: "))
    print ("Your temperature in Celsius is: ")
    print ((initCvalue2 - 32) * (5/9), "C")
    elif FtoC == 2:
    initFvalue = float(input("Enter temperature in Celsius: "))
    print ("Your temperature in degrees Farenheit is: ")
    print (initFvalue * (9/5) +32, "F")

    NewCalculation= int(input ("Do you want to do another conversion (1) or go back to the main menu (2)? (Choose 1 or 2): "))
    if NewCalculation == 1:
    continue
    else:
    CalcApp()

    def unitConverter():
    while True:
    ImpSci = int(input("Do you want to convert from an imperial unit to a SI unit (1) or from a SI unit to an imperial unit (2)? Enter option (1 or 2): "))

    if ImpSci == 1:
    print ("Convert from: ")
    print ("1. Inches")
    print ("2. Feet")
    print ("3. Miles")
    unit1=int(input("Enter option (1,2,3): "))
    print ("Convert to: ")
    print ("1. Centimetres")
    print ("2. Meters")
    print ("3. Kilometers")
    unit2=int(input("Enter second option (1,2,3): "))
    value1 = float(input(f"Enter value (in first unit) to be converted: "))

    if unit1 == 1 and unit2 ==1:
    print (f"{value1} in centimetres is: ")
    print (value1 * 2.54, "cm")
    elif unit1 == 1 and unit2 ==2:
    print (f"{value1} in meters is: ")
    print (value1 / 39.37, "m")
    elif unit1 == 1 and unit2 ==2:
    print (f"{value1} in kilometers is: ")
    print (value1 / 39370, "km")
    elif unit1 == 2 and unit2 ==1:
    print (f"{value1} in centimetres is: ")
    print (value1 * 30.48, "cm")
    elif unit1 == 2 and unit2 ==2:
    print (f"{value1} in metres is: ")
    print (value1 / 3.281, "m")
    elif unit1 == 2 and unit2 ==3:
    print (f"{value1} in kilometres is: ")
    print (value1 / 3281, "km")
    elif unit1 == 3 and unit2 ==1:
    print (f"{value1} in centimetres is: ")
    print (value1 * 160900, "cm")
    elif unit1 == 3 and unit2 ==2:
    print (f"{value1} in metres is: ")
    print (value1 / 3.1609, "m")
    elif unit1 == 3 and unit2 ==3:
    print (f"{value1} in kilometres is: ")
    print (value1 * 1.609, "km")
    else: print ("Error, wrong input.")
    elif ImpSci == 2:
    print ("Convert from: ")
    print ("1. Centimetres")
    print ("2. Meters")
    print ("3. Kilometers")
    unit1=int(input("Enter option (1,2,3): "))
    print ("Convert to: ")
    print ("1. Inches")
    print ("2. Feet")
    print ("3. Miles")
    unit2=int(input("Enter second option (1,2,3): "))
    value1 = float(input(f"Enter value (in first unit) to be converted: "))

    if unit1 == 1 and unit2 ==1:
    print (f"{value1} in inches is: ")
    print (value1 / 2.54, "inches")
    elif unit1 == 1 and unit2 ==2:
    print (f"{value1} in feet is: ")
    print (value1 / 30.48, "feet")
    elif unit1 == 1 and unit2 ==2:
    print (f"{value1} in miles is: ")
    print (value1 / 160900, "miles")
    elif unit1 == 2 and unit2 ==1:
    print (f"{value1} in inches is: ")
    print (value1 * 39.37, "inches")
    elif unit1 == 2 and unit2 ==2:
    print (f"{value1} in feet is: ")
    print (value1 * 3.281, "feet")
    elif unit1 == 2 and unit2 ==3:
    print (f"{value1} in miles is: ")
    print (value1 / 1609, "miles")
    elif unit1 == 3 and unit2 ==1:
    print (f"{value1} in inches is: ")
    print (value1 * 39370, "inches")
    elif unit1 == 3 and unit2 ==2:
    print (f"{value1} in feet is: ")
    print (value1 * 3281, "feet")
    elif unit1 == 3 and unit2 ==3:
    print (f"{value1} in miles is: ")
    print (value1 / 1.609, "miles")
    else:
    print ("Error, wrong input.")
    else:
    print ("Error, wrong input.")

    NewCalculation= int(input ("Do you want to do another conversion (1) or go back to the main menu (2)? (Choose 1 or 2): "))
    if NewCalculation == 1:
    continue
    else:
    CalcApp()



    def CalcApp():
    while True:
    print ("Commands: ")
    print ("1. Regular calculator")
    print ("2. Kelvin converter")
    print ("3. Farenheit converter")
    print ("4. Unit converter")
    print ("5. Quit")
    calcType= int(input ("Choose type of calculator (1, 2, 3, 4 or 5): "))

    if calcType == 1:
    mainCalc()
    elif calcType == 2:
    KelvinConverter()
    elif calcType == 3:
    FConvert()
    elif calcType == 4:
    unitConverter()
    elif calcType == 5:
    quit()
    else:
    print ("Error, input not valid")

    CalcApp()

    Continue reading...

Compartilhe esta Página