Skip to content

Instantly share code, notes, and snippets.

@chicagowebmanagement
Last active May 14, 2019 23:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chicagowebmanagement/edd7037b184ff46d1ca5333c82051c32 to your computer and use it in GitHub Desktop.
Save chicagowebmanagement/edd7037b184ff46d1ca5333c82051c32 to your computer and use it in GitHub Desktop.
trying to understand classes better
"""
ERRORS
A. print(Triangle.mytriangle_area(self)) = NameError: name 'self' is not defined
B. print(Triangle.mytriangle_area()) = TypeError: mytriangle_area() missing 1 required positional argument: 'self' (SEE LINE with comments BELOW)
C. print(Triangle.mytriangle_area) = prints out '<function Triangle.mytriangle_area at 0x7f8f1a5fb620>'
"""
# ----------------------------------------------
import math
class Triangle:
def __init__(self, base, height):
self.base = base
self.height = height
def triangle_area(self):
return (self.base/2)*self.height
goofy = Triangle(23.5, 35.4)
print(goofy.triangle_area())
@chicagowebmanagement
Copy link
Author

Line 10 - You can remove the () after 'Triangle'
Line 11- add a space after each comma
Line 12 - add a space on either side of '='
Line 13 - same as line 12
Line 16 - add a space on either side of '/' and '*'
Line 19 - add a space on either side of '=' and after the comma
Errors:
A. you don't put 'self' in the arguments when calling it.
B. You never defined Triangle in the program outside of the class. You would have to put a base and height just like in line 19 when you defined a variable using the Triangle class.
C. You are asking to print the memory location of where the function mytriangle_area is residing inside of the class Triangle instead of trying to run the function from the class.
About the self.base and self.height. That is (according to my limited understanding) the function inside the class Triangle to associate those with the arguments from the init function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment