Python Forum
OOP hellp (simple class person)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
OOP hellp (simple class person)
#1
Hello friends, I have started to learn OOP in python and I want some help with the simple class. I am trying to create a simple "person" class and return the birth year of the person. so I am pasting in my atemmt and the error I am geting below.

class Person:

	def __init_(self, name, age):
		self.name = name
		self.age = age 
		self.year = 2020

	def get_birth_year(self, name, age):
		print("hello" + " " + self.name)
		print("you are bourn")
		result = self.year - self.age
		print(result)


print("enter your name")
name = input()
print("enter your age")
age = input()
p1 = Person()
print(p1.get_birth_year(name,age))
Error:
Traceback (most recent call last): File "person.py", line 20, in <module> print(p1.get_birth_year(name,age)) File "person.py", line 9, in get_birth_year print("hello" + " " + self.name) AttributeError: 'Person' object has no attribute 'name'
Thanks for help it mite be something stupid or incorrect logick, but I am trying to learn OOP in python and replicate what i learned in java 5 years ago.
Reply
#2
Two problems:

  1. There's a typo in the __init__() function signature. You have __init_ instead of __init__. Missing a trailing underscore.
  2. When you instantiate p1, you'll need two arguments for Person().
Reply
#3
I recommend that you check into python3's f-string.
That would turn this print("hello" + " " + self.name) into this print(f"hello {self.name}")
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#4
Also def get_birth_year(self, name, age): should not have parameters name & age, and it should return result instead of printing it
Reply
#5
(Jul-14-2020, 03:28 PM)stullis Wrote: Two problems:

  1. There's a typo in the __init__() function signature. You have __init_ instead of __init__. Missing a trailing underscore.
  2. When you instantiate p1, you'll need two arguments for Person().

Thanks i will look in to this. Yep spelling mistakes hapins for me more often then i want them, using a screenreader when you are coding has some cons and that one is for sure...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  A file for every person of a list Emmanouil 3 2,273 Aug-29-2019, 07:19 AM
Last Post: perfringo
  Discord Bot - If Person Mentions Role badmuchachob 0 2,188 Jan-30-2019, 06:39 PM
Last Post: badmuchachob
  Class Object hellp Dneal620 1 2,050 Dec-18-2018, 12:19 AM
Last Post: micseydel
  Installing Homebrew on my mac; non computer-savvy person duongtn34 1 1,860 Oct-23-2018, 09:11 AM
Last Post: Larz60+
  Why Person() takes no arguments dianefly 3 30,928 Oct-22-2018, 04:15 PM
Last Post: dianefly
  Installing Homebrew on my mac; non computer-savvy person hohen036 3 3,782 Oct-12-2018, 11:02 PM
Last Post: Larz60+
  Can you tell me where this person got the version of Python that he's using? nelsonkane 8 5,003 Mar-11-2018, 11:10 AM
Last Post: gptura
  problem with simple class code diegoraffo 5 3,618 Jan-27-2018, 02:31 AM
Last Post: ka06059
  Troubleshooting simple script and printing class objects Drone4four 11 8,031 Dec-16-2017, 05:12 AM
Last Post: Drone4four

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020