Python Forum
Need Help to understand class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need Help to understand class
#1
I write this code & run perfect

class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

  def myfunc(self):
    print("Hello my name is " + self.name)

p1 = Person("John", 36)
p2 = Person("Roshan",45)

p1.myfunc()
p2.myfunc()
but when i want to print age in myfunc then program gives me error

class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

  def myfunc(self):
    print("Hello my name is " + self.name)
    print("My age is " + self.age)
    

p1 = Person("John", 36)
p2 = Person("Roshan",45)

p1.myfunc()
p2.myfunc()
what is wrong with me ?
Reply
#2
It would be a lot more helpful if you posted full error traceback message in error tags.
But I assume the problem is in type of age variable, which is an integer. You can't concatenate string and integer directly.
Reply
#3
(Jun-05-2018, 08:50 AM)Roshan Wrote: what is wrong with me ?
:-) There is nothing wrong with you, I hope... There is problem that you cannot add/concatinate str and int.
Please, always post the entire traceback that you get. Take a time to read What to include in a post.
That said it's better to get use of more advanced string formatting features
def myfunc(self):
    print("Hello my name is {}".format(self.name))
    print("My age is {}".format(self.age))
if you are on python 3.6+ you can even use the f-strings
def myfunc(self):
    print(f"Hello my name is {self.name}.")
    print(f"My age is {self.age}.")
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
self.name is an instace of str
self.age is an instance of int

You can't add str with int. First you need to convert the integer to a string, with the str() method.
Better is, when you use string formatting: https://pyformat.info/

print('Hello {}. You are {} old'.format(self.name, self.age))
You can do also a little trick:
print('Hello {self.name}. You are {self.age} old'.format(self=self))
Or you just use the newest Python version (3.6.5) with format string interpolation.
print(f'Hello {self.name}. You are {self.age} old')
# look at the f in front of the string.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
Thanks to point problem. That was int .
I change it to string at then time of printing.

def myfunc(self):
    print("Hello my name is " +  self.name)
    print("My age is " + str(self.age))
    
Thanks
Reply
#6
This is most primitive way of creating string, better use more advanced one as we show you. String formatting is very powerful
https://docs.python.org/3/library/string...i-language
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Forum Jump:

User Panel Messages

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