These are chat archives for andela/andela-21-lite

7th
Jun 2016
clarch
@clarch
Jun 07 2016 07:41
Hi guys, anyone help me out to a function to increament the age
class Person:
    def __init__(self,initialAge):
        # Add some more code to run some checks on initialAge
        self.initialAge = initialAge
        if (initialAge < 0):
            initialAge = 0
            print "Age is not valid, setting age to 0."
        initialAge = age

sorry this is the whole on

class Person:
    def __init__(self,initialAge):
        # Add some more code to run some checks on initialAge
        self.initialAge = initialAge
        if (initialAge < 0):
            initialAge = 0
            print "Age is not valid, setting age to 0."
        initialAge = age
     def yearPasses(self):
        # Increment the age of the person in here
        age = age + 1

this is wrong because i cannot access age in my yearPasses() so how do i make sure I can access it

Michael Gichia
@michaelgichia
Jun 07 2016 08:08

Hi @clarch , to access yearsPasses() first initialize(lack for a better word) Person.

example:

tim = Person(21)

were tim is the person you've assigned to age 21.

The method yearPasses() needs an attribute (hope what I'm thinking is what you are trying to achieve)

example:
    def yearPasses(self, year):
        # Increment the age of the person in here
to access years passed then will be
tim.yearPasses(2)
Michael Gichia
@michaelgichia
Jun 07 2016 08:15
Please expound on what the class does so i can understand better what you are trying to achieve.
clarch
@clarch
Jun 07 2016 08:22
@michaelgichia ok Person() class has yearPasses() function which increaments the age by 1 and for now the yearPasses() is not working this is the error.
Traceback (most recent call last):
  File "solution.py", line 27, in <module>
    p.yearPasses()        
  File "solution.py", line 20, in yearPasses
    age = age + 1
UnboundLocalError: local variable 'age' referenced before assignment
Michael Gichia
@michaelgichia
Jun 07 2016 09:28

Increamenting age then try assigning to self.initialAge

self.initialAge = age + 1

and you haven't return the value of age also

return self.initialAge
@clarch on your code look at line 20.
age = age + 1
clarch
@clarch
Jun 07 2016 10:27
:smile: thanks @michaelgichia seems to work, I have a question though do I need to use self.initialAge again in yearPasses() if I already assigned it to age in my init, is there another way I can reference age outside of init
Michael Gichia
@michaelgichia
Jun 07 2016 10:44
Yes you do. init method is initialization of the object.
Michael Gichia
@michaelgichia
Jun 07 2016 10:50
Example: when a child is born we give this child a name. We refer to the child using the names he or she was given. For you to use or interact with the child(object) we must pass assign them the attribute name. As in your case, the attribute one needs to assign before interacting with person is initialAge.
So you can refer to the attributes as long as you need it.
You will refer to it as self.initialAge. It is globally available in class.
clarch
@clarch
Jun 07 2016 12:32
oh yea, makes sense, got it thanks :+1:
Michael Gichia
@michaelgichia
Jun 07 2016 12:34
anytime
clarch
@clarch
Jun 07 2016 12:39
oh wait the error disappears but it doesn't seem to increment the age though
Michael Gichia
@michaelgichia
Jun 07 2016 12:40
can I see your code?
clarch
@clarch
Jun 07 2016 13:14
actually it works when I use self.initialAge instead of assigning it to age like so
class Person:
    def __init__(self,initialAge):
        # Add some more code to run some checks on initialAge
        self.initialAge = initialAge
        if (initialAge < 0):
            initialAge = 0
            print "Age is not valid, setting age to 0."
    def yearPasses(self):
        # Increment the age of the person in here        
        self.initialAge = self.initialAge + 1

p = Person(10)  
p.amIOld()
for j in range(0, 3):
    p.yearPasses()        
p.amIOld()
print("")
Michael Gichia
@michaelgichia
Jun 07 2016 13:17
This message was deleted
This message was deleted
I'm not understanding p.amIOld() part.
Michael Gichia
@michaelgichia
Jun 07 2016 13:24
Give me few minutes I go through it.
clarch
@clarch
Jun 07 2016 13:30
oh sorry hehe that is another function that i was trying out
i think for me to use 'age' as I was, it needs to be a global variable, wah these things usually confuse me, is it just me hehe?
Michael Gichia
@michaelgichia
Jun 07 2016 13:37

Condition should come before assignment.

        if (initialAge < 0):
            initialAge = 0
            print "Age is not valid, setting age to 0."

The above snippet should come before assigning self.initialAge = self.initialAge so that if it doesn't meet the condition then it won't be assigned and the error can be logged back.

class Person:
    def __init__(self,initialAge):
        # Add some more code to run some checks on initialAge
        if (initialAge < 1):
            print("Age must be above 0")
        self.initialAge = initialAge
The above code snippet will make sure the condition is met first before assigning self.initialAge = initialAge.
Michael Gichia
@michaelgichia
Jun 07 2016 13:46
To increment age then call the increment method .
in your case it is Person().yearPasses().

If you look at your method

    def yearPasses(self):
        # Increment the age of the person in here        
        self.initialAge = self.initialAge + 1

you have not returned self.initialAge thus you won't get any value.

    def yearPasses(self):
        # Increment the age of the person in here        
        self.initialAge = self.initialAge + 1
        return self.initialAge
Michael Gichia
@michaelgichia
Jun 07 2016 13:53

I would recommended you revisit Class/Objects and OOP.

The tutorials are well explained and you will understand better.

clarch
@clarch
Jun 07 2016 14:34
simple and nice, thanks :+1:
Walusimbi Mahad
@andela-engmkwalusimbi
Jun 07 2016 15:14
It is globally available in class. @michaelgichia @clarch it's not global per say, it's set as an instance
class Person:
    def __init__(self,initialAge):
        # Add some more code to run some checks on initialAge
        if (initialAge < 1):
            print("Age must be above 0")
        self.initialAge = initialAge
@michaelgichia this won't deny self.initialAge to equal to whatever value that a user passed in.
Perhaps you might want to do it like the below snippet
class Person:
    def __init__(self,initialAge):
        # Add some more code to run some checks on initialAge
        if (initialAge < 1):
                    initialAge = 1
                    # may be you change the message printed out
                    print("Age must be above 0 ")
        self.initialAge = initialAge
Walusimbi Mahad
@andela-engmkwalusimbi
Jun 07 2016 15:23

For this method you actually don't need to return self.initialAge . Since it's an attribute of that class, you can access initialAge by using the object/instance of the class
```def yearPasses(self):

    # Increment the age of the person in here        
    self.initialAge = self.initialAge + 1
    return self.initialAge```

i.e.

mahad = Person(2)
mahad.yearPasses()
print mahad.initalAge
Michael Gichia
@michaelgichia
Jun 07 2016 15:57
@andela-engmkwalusimbi thanks for the clarification.
Walusimbi Mahad
@andela-engmkwalusimbi
Jun 07 2016 15:57
@michaelgichia thanks for the good work