Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 局部变量;年龄“;分配前参考_Python_Variables - Fatal编程技术网

Python 局部变量;年龄“;分配前参考

Python 局部变量;年龄“;分配前参考,python,variables,Python,Variables,我最近开始学习python。我正在解决一个问题 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

我最近开始学习python。我正在解决一个问题

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 ("") 
输出必须如下所示:

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.

你能告诉我我做错了什么吗?。谢谢

在python中,您必须显式地使用
self
来访问实例属性:

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
班级人员:
定义初始年龄(自我、初始年龄):
如果初始年龄<0:
self.age=0
打印(“此人无效,将年龄设置为0。”)
其他:
self.age=初始年龄
def amIOld(self):

如果在python中self.age=13且self.age,则必须显式使用
self
来访问实例属性:

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
班级人员:
定义初始年龄(自我、初始年龄):
如果初始年龄<0:
self.age=0
打印(“此人无效,将年龄设置为0。”)
其他:
self.age=初始年龄
def amIOld(self):

如果python中的self.age=13且self.age,则必须显式地使用selfself来访问实例方法中的属性,类似于java中的'this'

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("")
班级人员:
定义初始值(自身,初始值):
#添加更多代码以对初始值运行一些检查
self.initialAge=初始值
如果初始值<0:
打印(“年龄无效,将年龄设置为0。”)
初始值==0
def amIOld(self):
#在这里进行一些计算,并将正确的语句打印到控制台
如果self.initialAge=13且self.initialAge为17:
打印(“你老了。”)
def年通行证(自我):
#增加这里人的年龄
self.initialAge+=1
#打印(“年龄增加”)
t=int(输入())
对于范围(0,t)内的i:
年龄=整数(输入()
p=人(年龄)
p、 阿米奥德()
对于范围(0,3)内的j:
p、 年票()
p、 阿米奥德()
打印(“”)

在python中,您必须显式地使用self来访问实例方法中的属性,类似于java中的this

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("")
班级人员:
定义初始值(自身,初始值):
#添加更多代码以对初始值运行一些检查
self.initialAge=初始值
如果初始值<0:
打印(“年龄无效,将年龄设置为0。”)
初始值==0
def amIOld(self):
#在这里进行一些计算,并将正确的语句打印到控制台
如果self.initialAge=13且self.initialAge为17:
打印(“你老了。”)
def年通行证(自我):
#增加这里人的年龄
self.initialAge+=1
#打印(“年龄增加”)
t=int(输入())
对于范围(0,t)内的i:
年龄=整数(输入()
p=人(年龄)
p、 阿米奥德()
对于范围(0,3)内的j:
p、 年票()
p、 阿米奥德()
打印(“”)
Sidenote(我认为这会对您有所帮助,因为您正在学习):更多地关注代码样式,请检查。例如,使用
initial_age
而不是
initial_age
,使用驼峰大小写的方法名称(
am_i_old
year_passed
),无需添加“;”在行尾,不要使用短变量,如
t
p
。旁注(我认为这可能很有用,因为您正在学习):更多地注意代码样式,检查。例如,使用
initial_age
而不是
initial_age
,使用驼峰大小写的方法名称(
am_i_old
year_passed
),无需添加“;”在行尾,不要使用短变量,如
t
p