-1
我正在製作一個程序,從文件中讀取數字和名稱,並將它們變成Person對象。其方法應該能夠顯示姓名或號碼,或所有人的姓名和號碼。但由於某種原因,我陷入了我的環境類與一個語法錯誤,其中我在y'= 2'在填充方法內的while循環中增加y。在Python中的語法錯誤...我不明白爲什麼
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")
什麼是語法錯誤? – Blender
剛剛編輯,其描述 – Zach
我認爲縮進錯誤是複製/粘貼到堆棧溢出的結果? – mgilson