0

I recently started learning python. And im working on a problem.

class Person:
    age = 0
    def __init__(self,initial_Age):
        if initial_Age<0:
            age=0
            print("This person is not valid, setting age to 0.")
        else:
            age = initial_Age

    def amIOld(self):
        if(age<13):
            print("You are young.")
        elif(age>=13 and age<18):
            print("You are a teenager.")
        else:
            print("You are old.")
    def yearPasses(self):
        age = age + 1
T=int(input())
for i in range(0,T):
age=int(input())         
p=Person(age)  
p.amIOld()
for j in range(0,3):
    p.yearPasses();        
p.amIOld();
print ("") 

The error I get is displayed below:

Traceback (most recent call last):
File "solution.py", line 27, in <module>
p.yearPasses();        
File "solution.py", line 19, in yearPasses
age = age + 1 
UnboundLocalError: local variable 'age' referenced before assignment

The Input for this goes like:

4(Number of test cases)
-1
10
16
18

The output has to be something like this:

This person is not valid, setting age to 0.
You are young.
You are young.

You are young.
You are a teenager.

You are a teenager.
You are old.

You are old.
You are old.

Can you please guide me to what Im doing wrong?. Thanks

fahadkaleem
  • 73
  • 1
  • 7
  • Sidenote (I think it can be useful since you are learning): put more attention to the code style, check [the style guide](https://www.python.org/dev/peps/pep-0008/). For example, use `initial_age` instead of `initial_Age`, use camel cased method names (`am_i_old`, `year_passed`), no need to add ';' at the end of line, don't use short variables like `T` and `p`. – Borys Serebrov Jan 20 '16 at 19:27

2 Answers2

8

In python you have to use explicitly self to access instance attributes:

class Person:
    def __init__(self, initial_Age):
        if initial_Age < 0:
            self.age = 0
            print("This person is not valid, setting age to 0.")
        else:
            self.age = initial_Age

    def amIOld(self):
        if self.age<13:
            print("You are young.")
        elif self.age>=13 and self.age<18:
            print("You are a teenager.")
        else:
            print("You are old.")

    def yearPasses(self):
        self.age += 1
Daniel
  • 42,087
  • 4
  • 55
  • 81
0

In python you have to use explicitly self to access attributes from instance methods similar to 'this' in java

class Person:
def __init__(self,initialAge):
    # Add some more code to run some checks on initialAge
    self.initialAge = initialAge
    if initialAge < 0:
        print("Age is not valid, setting age to 0.")
    initialAge == 0 

def amIOld(self):
    # Do some computations in here and print out the correct statement to the console
    if self.initialAge<13:
        print("You are young.")
    elif self.initialAge>=13 and self.initialAge<18:
        print("You are a teenager.")
    elif self.initialAge>17:
        print("You are old.")

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

t = int(input())
for i in range(0, t):
age = int(input())         
p = Person(age)  
p.amIOld()
for j in range(0, 3):
    p.yearPasses()       
p.amIOld()
print("")