我正在制作一个程序,它从文件中读取数字和名称并将它们转换为 Person 对象。它的方法应该能够显示姓名或号码,或所有人的姓名和号码。但是由于某种原因,我陷入了我的 Environment 类中,出现语法错误,我在 populate 方法内的 while 循环中增加了 'y+=2' 处的 y。
class Person (object):
pplCnt = 0
def __init__ (self,name,number):
print("Creating a person named ", name, ", phone number: ", number)
self.name = name
self.number = number
Person.pplCnt+=1
def getName(self):
return self.name
def getNum(self):
return self.number
def getAmtPpl(self):
return Person.pplCnt
class Environtment:
x=0
y=0
def __init__(self):
self.pplList = []
def populate(self):
people = open("names.txt","r")
listof = []
for line in people:
listof.append(line)
x+=1
while y<=x:
self.pplList.append(Person(listof[y],listof[y+1])
y+=2
def nameAll(self):
z=0
while z < int(x/2):
print("Name: ",self.pplList[z].getName(),"\nNumber: ",self.pplList[z].getNum(),"\n")