3
\$\begingroup\$

I made a program for my calculator for A levels that acquires the value of the angle and radius of a sector and gives:

  1. Area of Sector
  2. Area of inner Triangle
  3. Arc Length
  4. Chord Area
  5. Chord Length
  6. Sector Perimeter

I need some advice on compacting and making it more efficient, it would be also nice to have some coding tricks.

Disclaimer: my calculator uses micropython which lacks alot of functions, and modules however some functions from math work, check the documentary about it to confirm.

from math import pi, sin

def input_pi_replacer(prompt):
    return float(eval(input(prompt).replace("pi", str(pi))))

def stopper():
    stop_or_continue = input("Stop?: ")
    if stop_or_continue == "x":
        raise SystemExit  

print("Sector Quantities\nCalculator:\n")
C_Factor = 0
Area = 0
B = 0
while True:
    C_Factor = 0
    Area = 0

    Unit = ""
    # + = degrees , - = radians
    while Unit not in ['+', '-']:
        Unit = input("For Deg, enter '+',\nFor Rad, enter '-'\n: ")
        if Unit == "+":
            C_Factor = pi/180
            break
        elif Unit == "-":
            C_Factor = 180/pi
            break
        elif Unit == ".":
            raise SystemExit
    # R = Radius, A = theta/angle in deg/rads, C_Angle = Converted Angle, C_Factor = Conversion Factor of Angle

    R = input_pi_replacer("\nEnter Radius: ")
    A = input_pi_replacer("Enter Theta/Angle: ")

    if Unit == "+":
        C_Angle = A * C_Factor
        Angle_In_Deg = A
    elif Unit == "-":
        C_Angle = A
        Angle_In_Deg = A * C_Factor

    print()
    Sector_Area = 1/2 * (R**2) * C_Angle
    Triangle_Area = 1/2 * (R**2) * sin(C_Angle)
    Arc_Length = R * C_Angle
    Chord_Area = Sector_Area - Triangle_Area
    Chord_Length = 2 * R * sin(C_Angle/2)
    Perimeter_Sector = 2 * R + Arc_Length
    print("Area of Sector\n= ", Sector_Area)
    print("Area of inner Triangle\n= ", Triangle_Area)
    print("Arc Length\n= ", Arc_Length)
    print("Chord Area\n= ", Chord_Area)
    print("Chord Length\n= ", Chord_Length)
    print("Sector Perimeter\n= ", Perimeter_Sector)
    print()
    stopper()
\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$

Naming

Variable and function names should be snake_case, not This_Format or This. Uppercase and PascalCase are reserved for class names.

Checking inside lists vs strings

If you're checking for a single character, it's better to use a string rather than a list. Have a look:

if unit in "+-":
    ...

The walrus operator

If you're using python-3.8 you can use the walrus operator. It's another way of saying assignment expressions. It's a way to assign a variable within an expression. In your case, you can utilize the walrus operator to assign input to a variable, and use that. Have a look:

while unit := input("For Degrees, enter '+',\nFor Radians, enter '-'\n: "):
    if unit in "+-":
        ...

So unit is assigned whatever the output of the input call is.

Unneeded function

If your function is simply an input call, you don't really need it. Just put it at the end of the while loop.

Ternary operators

You can use these to greatly shorten the length of your code. It's an easier way to assign values based on conditions. Take a look at the final code to see what I'm saying.

f-strings

You should use f"" to directly implement variables in your strings. It's a cool feature that keeps you from having to + or call a .format on your strings. Have a look:

print(f"Area of Sector = {sector_area}")
print(f"Area of inner Triangle = {triangle_area}")
...

Final code

After all these suggestions, you code would look something like this:

from math import pi, sin

def input_pi_replacer(prompt):
    return float(eval(input(prompt).replace("pi", str(pi))))

print("Sector Quantities Calculator:\n")
while True:

    while unit := input("For Degrees, enter '+',\nFor Radians, enter '-'\n: "):
        if unit in "+-.":
            if unit == ".":
                raise SystemExit
            c_factor = (pi / 180) if unit == "+" else (180 / pi)
            break

    radians = input_pi_replacer("\nEnter Radius: ")
    angle = input_pi_replacer("Enter Theta/Angle: ")

    c_angle = angle * c_factor if unit == "+" else angle

    print()
    sector_area = 1 / 2 * (radians ** 2) * c_angle
    triangle_area = 1 / 2 * (radians ** 2) * sin(c_angle)
    arc_length = radians * c_angle
    chord_area = sector_area - triangle_area
    chord_length = 2 * radians * sin(c_angle / 2)
    perimeter_sector = 2 * radians + triangle_area

    print(f"Area of Sector = {sector_area}")
    print(f"Area of inner Triangle = {triangle_area}")
    print(f"Arc Length = {arc_length}")
    print(f"Chord Area = {chord_area}")
    print(f"Chord Length = {chord_length}")
    print(f"Sector Perimeter = {perimeter_sector}")
    print()

    if input("Stop?: ").lower() == "x":
        raise SystemExit  
\$\endgroup\$
1
  • \$\begingroup\$ hmm walrus operator, thats new, and fyi micropython doesn't support f" strings, its sad i know, however thanks for the rest!! \$\endgroup\$
    – Anonymous
    Commented Mar 31, 2020 at 23:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.