CODE PROJECT

Link to code: https://replit.com/@royzeng27/Roys-calculator#main.py

For this project, I chose to write a code that could perform mathematical operations. I integrated three modes into the calculator, being quadratic equation calculator, linear equation calculator, and Pythagorean theorem calculator.

– The flowchart of the code.

PT.1: FUNCTIONS

 def disc(a,b,c):
    return b**2 - 4 * a * c
  def hypo(a3, b3):
    cf_1 = (a3**2 + b3**2)
    cf_1f = (cf_1**0.5)
    return cf_1f
  def legcalc(a4, c4):
    cf_2 = (c4**2 - a4**2)
    cf_2f = (cf_2**0.5)
    return cf_2f

To begin with, I set a few functions that would be used in the code later on, specifically for the quadratic calculator and the Pythagorean Theorem calculator.

PT.2: TYPE SELECT

while True:  #This is used to start and infinite loop, so the user can keep using the calculator without having to restart the program.
  type_select = input(
      'type 1 for quadratic equation calculator, type 2 for linear equation calculator, type 3 for pythagorean theorem calculator, and type 4 to end program:  '
  )  #The variable type_select is used to determine which calculator the user wants to use.

I started off with a piece of code where the user can input a number from one to four to indicate the mode of calculator that the user wants. I used a “while true” infinite loop to make sure that the user can use the code repeatedly without having to restart the code again. The code will terminate if the user inputs four for the type_select variable.

PT.3: QUADRATIC EQUATION

if type_select == '1':
    try:  # The try - except system eliminates errors, as an invalid variable would just redirect the user to the beginning of the program.
      print(
          'ax^2 + bx + c = 0'
      )  #This is the quadratic equation, so that the user knows what the variables are.
      a = int(input('a value is:  '))
      b = int(input('b value is:  '))
      c = int(input('c value is:  '))

If the user types one, the quadratic equation code runs. To begin with, the user is given the template for the quadratic equation, with variables a, b, and c in the template, that helps the user find the numbers to input into the calculator. After the equation is displayed, the user can input the three values needed to calculate the answer, a, b, and c.

if a == 0:  #This condition checks if the user inputted a value for a. If they did not, the program will show that a cannot be 0.
        print('a cannot be 0')
      discriminant = disc(a,b,c)
      if discriminant < 0:  #This condition checks if the discriminant is less than 0. If it is, the program will show that there are no solutions.
        print('no solution: the discriminant is negative')

The next block of code checks the user’s inputs for whether they are valid or not before the execution of the code. The code does this by checking if the a value is 0 or not or if the discriminant is negative or not using the disc function. If either of them is indicative of an erroneous input, the code outputs no solution and returns the user back to the start of the code.

 else:  #If the variables inputted are valid, the program will calculate the solution using the quadratic formula.
        solution_1a = (-b + discriminant**0.5)
        solution_1f = (solution_1a / (2 * a))
        print('x1 = ', solution_1f)
        solution_2a = (-b - discriminant**0.5)
        solution_2f = (solution_2a / (2 * a))
        print('x2 = ', solution_2f)
    except:  # if the user inputs a value that is not an integer, the program will show that the input is invalid.
      print('invalid equation')

If the numbers inputted are valid, the code calculates the answer using the quadratic formula and prints out the answers at the end. By using “except,” it also helps protect the code from word inputs by redirecting the user to the beginning and outputting “invalid equation.”

PT. 4: LINEAR EQUATION

if type_select == '2':
    try:
      print(
          'ax+c = 0'
      )  #This is the basic structure of the linear equation, where the variables are to the power of one.
      a2 = int(input('a value is:  '))
      c2 = int(input('c value is:  '))

If the user inputs two, then the linear equation code will be executed. The format for the linear equations is displayed, and then then the user can input values for a and c in accordance with the format.

 if a2 == 0 and c2 != 0:  #This checks if the equation is invalid or not.
        print('no solution: ', c2, ' is not equal to 0')
      if a2 == 0 and c2 == 0:  #This checks if the equation has infinite equations or not.
        print('infinite solutions')

The next part checks for inputs that either have no solution or have infinite solutions by using the properties of linear equations.

else:  #If the equation is valid, the program will calculate the solution.
        solution_l = (c2 / -a2)
        print('x = ', solution_l)
    except:
      print(
          'invalid equation'
      )  #If the user inputs a value that is not an integer, the program will show that the input is invalid.

If the inputs are valid and have a finite amount of solutions, the code will calculate the answer to the inputted equation. The use of “except” also helps protect the code from word inputs by redirecting the user to the beginning and outputting “invalid equation.”

PT. 5: PYTHAGOREAN CALCULATOR

if type_select == '3':
    try:
      p_input_select = input(
          'type 1 if the hypotenuse is unknown and type 2 if the leg is unknown:'

If the user inputted three at the start, the Pythagorean theorem calculator will run. The user chooses between the unknown being the hypotenuse or the unknown being a leg of the triangle by typing either one or two.

if p_input_select == '1':  #This is if the user wants to know the hypotenuse.
        print('a^2 + b^2 = c^2')
        a3 = int(input('a value is:  '))
        b3 = int(input('b value is:  '))
        print('the hypotenuse is:  ', hypo(a3,b3)
              )  #This calculates the length by using the pythagorean theorem.

If the user inputs one, the code uses the hypo function defined earlier to calculate the missing hypotenuse.

 if p_input_select == '2':  #This is if the user wants to know the leg.
        print('a^2 + b^2 = c^2')
        a4 = int(input('a value is:  '))
        c4 = int(input('c value is:  '))
        print('the leg is:  ', legcalc(a4,c4)
              )  #This calculates the length by using the pythagorean theorem.
    except:
      print('invalid equation')

If the user inputs two, the code uses the leg calc function to calculate the missing leg of the triangle. The use of “except” also helps protect the code from word inputs by redirecting the user to the beginning and outputting “invalid equation.”

PT. 6: END PROGRAM

if type_select == '4':  #To end the program, the user can input four at the start of the program.
    print('program terminated')
    break  #This breaks the infinite loop, ending the program.

If the user inputs four at the beginning, the code comes to an end as a result of the use of a break.

PT. 7: REFLECTION

I am reluctant that the code does not have any bugs, as it contains the use of try, except, and if where the code checks for potential erroneous inputs. However, the code could be improved by allowing users to continue to use a specific type of calculator instead of having to choose the type again as they are redirected to the beginning of the code again.


Comments

5 Responses to “CODE PROJECT”

  1. mcrompton Avatar
    mcrompton

    Excellent job, Roy. You clearly know your stuff. My one question regards the definition and use of a function. You have not included any functions in your code. Can you edit the code to demonstrate that you know what you’re doing there? Please comment below when you resubmit.

    1. I edited the wording, and it should be alright now. Thanks a lot for pointing out the mistake!

      1. mcrompton Avatar
        mcrompton

        Getting closer, Roy! You are required to define and call a function. Can you please include this in your code and resubmit?

        1. rzeng27 Avatar
          rzeng27

          OK, I finished the coding, and I am resubmitting now.

          1. mcrompton Avatar
            mcrompton

            YAY!!!!!! Thank you, Roy.

Leave a Reply

Your email address will not be published. Required fields are marked *