Help i don't know what i did to this code

Can someone help me debug the algebra part in the code?

The code that send it back to the begging keeps breaking. HELP! IDK What I did wrong Can someone tell me what i possibly did wrong?

https://replit.com/@lucasfegarsky12/Advanced-Calculator

The code that sends it back to the begging is the Import main.py is there any other way to send it to the begging

can you put a screen shot of your issue?

You are not switching back to the main menu you are only exiting the Algebra.py. You can use a function in main.py that handles the calculation type selection and can be called again when switching is needed.

For example:

def main():
    print("What do you want to calculate? Shape related stuff or Algebra?")
    calculation_category = input("Enter 'Shape' or 'Algebra': ")

    if calculation_category.strip().lower() == "shape":
        import Volume_Stuff
        Volume_Stuff.main()
    elif calculation_category.strip().lower() == "algebra":
        import Algebra
        Algebra.main()
    else:
        print("Invalid input. Please enter 'Shape' or 'Algebra'.")
        main()  # Call main again for valid input

if __name__ == "__main__":
    main()

Im now gettting the error

Traceback (most recent call last):
  File "main.py", line 16, in <module>
    main()
  File "main.py", line 10, in main
    Algebra.main()
AttributeError: module 'Algebra' has no attribute 'main'

As the error said there’s no main attribute in your algebra.py
You just need to encapsulate the main att in your Algebra.py file.

Like this:

import math

def main(): #include this
    algebra_question = "Y"
    while algebra_question in ["Y", "y"]:
        equation = input("Enter the linear equation in the format 'ax+b=c': ")
        solution = solve_linear_equation(equation)
        print(f"The solution to the linear equation is: x = {solution}")
        algebra_question = input(
            "Do you want to continue with algebraic calculation? Press Y to continue, N to exit, or S to switch to shape calculator: "
        )

        if algebra_question in ["N", "n"]:
            shape_question = input(
                "Do you want to continue with the shape calculator? Press Y to continue or N to exit: "
            )
            if shape_question in ["Y", "y"]:
                import Volume_Stuff
                Volume_Stuff.main()
            else:
                print("Thank you for using the calculator!")
                break  # Exit the loop
        elif algebra_question not in ["Y", "y", "S", "s"]:
            print("Invalid input for algebraic choice. Please try again.")
1 Like
Traceback (most recent call last):
  File "main.py", line 16, in <module>
    main()
  File "main.py", line 10, in main
    Algebra.main()
  File "/home/runner/Advanced-Calculator/Algebra.py", line 5, in main
    solution = solve_linear_equation(equation)
NameError: name 'solve_linear_equation' is not defined

one of the errors was fixed though i figured out that the S part of the question is useless

You just need to make solve_linear_euqation accessible in the scope where you are trying to use it.

import math

def solve_linear_equation(equation):
    parts = equation.split("+")
    a = int(parts[0][:-1])
    b = int(parts[1].split("=")[0])
    c = int(parts[1].split("=")[1])
    solution = (c - b) / a
    return solution

def main():
    algebra_question = "Y"
    while algebra_question in ["Y", "y"]:
        equation = input("Enter the linear equation in the format 'ax+b=c': ")
        solution = solve_linear_equation(equation)
        print(f"The solution to the linear equation is: x = {solution}")
        algebra_question = input("Do you want to continue with algebraic calculation? Press Y to continue or N to exit to main: ")
        if algebra_question in ["N", "n"]:
            shape_question = input("Do you want to continue with the shape calculator? Press Y to continue or N to exit: ")
            if shape_question in ["Y", "y"]:
                import Volume_Stuff
            else:
                print("Thank you for using the calculator!")
        else:
            print("Invalid input for algebraic choice. Please try again.")
2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.