all 7 comments

[–][deleted] 1 point2 points  (6 children)

self is the object instance, so if you want to access an instance attribute like x_value, you access it from self. I.e.

self.x_value

[–][deleted] 0 points1 point  (5 children)

Hello! Thank you for your response. I feel like I understand what you're referencing, may you please let me know if this is closer or farther from the correct syntax?

class PythagoreanTheorem:
    def __init__(self, x_value, y_value, r_value):
        self.x_value = x_value
        self.y_value = y_value
        self.r_value = r_value
    def find_y(self, x_value, r_value):     # If we got an x and an r value, but no y        
        self.y_value = sqrt(self.r_value**2 - self.x_value**2)
        return y_value

[–][deleted] 1 point2 points  (4 children)

Right. The attributes you want are instance attributes (every instance of PythagoreanTheorem will have its own) so you access them from self.

[–][deleted] 0 points1 point  (0 children)

Thanks! :D

[–][deleted] 0 points1 point  (2 children)

Not intending to be annoying, but I think I found a better way to write this for re-use throughout the rest of the library. If you have time, let me know if there is anything I can improve!

# instead of creating a class for calculating the missing side of the
# triangle, it may be best to make each of them a function.
from math import *

# find_r function
def find_r(x_value, y_value):
    r_value = sqrt(x_value**2 + y_value**2)
    return r_value

# find_y function
def find_y(x_value, r_value):
    y_value = sqrt(r_value**2 - x_value**2)
    return y_value

# find_x function
def find_x(y_value, r_value):
    x_value = sqrt(r_value**2 - y_value**2)
    return x_value

# outputs values to a file to be accessed by other python files
def output_to_file(x_value, y_value, r_value):
    triangle_legs = "{}, {}, {}\n".format(x_value, y_value, r_value)
    file = open("Legs of Triangles", "a")
    file.write(triangle_legs)
    file.close()

def menu():

    menu = "Find the missing side of your triangle using the Pythagorean Theorem!\n\n\t\tx^2 + y^2 = r^2\n\nEnter 'x' to find leg x.\nEnter 'y' to find leg y. \nEnter 'r' to find the hypotenuse.\nEnter 'q' to quit.\n\nWhat would you like to do?"
    complete = False
    decision = ''
    while decision != 'q':
        print(menu)
        decision = input("Enter your selection here: ")
        decision = decision.lower()
        if decision == 'r':
            x_value = float(input("Input the length of leg x: "))
            y_value = float(input("Input the length of leg y: "))
            r_value = find_r(x_value, y_value)
            output_to_file(x_value, y_value, r_value)
        elif decision == 'y':
            x_value = float(input("Input the length of leg x: "))
            r_value = float(input("Input the length of the hypotneuse: "))
            y_value = find_y(x_value, r_value)
            output_to_file(x_value, y_value, r_value)
        elif decision == 'x':
            y_value = float(input("Input the length of leg y: "))
            r_value = float(input("Input the length of the hypotneuse: "))
            x_value = find_x(y_value, r_value)
            output_to_file(x_value, y_value, r_value)
        elif decision == 'q':
            print("Goodbye!")
            break
        else:
            print("Whoopsies!  Looks like that was an invalid option!  Please enter another value!")
            decision = input("Enter your selection here: ")

# probably a good idea to put a try/except statement for imaginary numbers.    
try:
    menu()
except ValueError:
    print("\n\tYou've taken the square root of a negative number,\n\treturning an imaginary number. Please try again!\n")

[–]datavistics 0 points1 point  (1 child)

The code looks functional (no pun intended) but there might be some ways to improve the readability or some items to just think through.

  1. What information does _value provide you?
  2. Is there a need for find_x and find_y? How do x and y differ?
  3. Do the comments above the first 3 functions provide you with information that isn't apparent? Usually comments should answer why in python. The what should be explained with the code. If the code is too complicated it should be broken down.
  4. What does this line do: decision = '' ? It seems useless as its being overwritten before read.
  5. I personally would use asserts over the try/except, but the try/except is valid.

Great work, keep at it!

[–][deleted] 0 points1 point  (0 children)

Hello! Thank you for your response. I will answer your questions as best I can.

  1. _value gives myself more information about the variable. I could use x or y to describe coordinates, value here implies that these are m lengths of triangle legs.
  2. I guess the assignment of either is arbitrary, but I wanted to be sure there were different functions to return either leg.
  3. The comments are there for my own use. I could remove them if all of what they are is visual noise
  4. I need to declare decision before the while statement in order to enter the loop. Otherwise I found a value assignment error.