我正在创建这个程序,它的功能之一是输出一个在构造函数中初始化的列表。但是,发生的事情是它以十六进制或其他形式输出内存位置,我不知道为什么。
我有两个班级和一个跑步班:
class Person :
def __init__(self, name, ID, age, location, destination):
self.name = name
self.ID = ID
self.age = age
self.location = location
self.destination = destination
def introduce_myself(self):
print("Hi, my name is " + self.name + " , my ID number is " + str(self.ID) + " I am " + str(self.age) + " years old")
def get_route(self):
return self.location + self.destination
def add2bus(self, Bus):
if Person.get_route(self) == Bus.get_route() :
Bus.get_on(Bus)
else :
print("not compatible")
def get_name(self):
print(self.name)
import People
class Bus :
def __init__(self, name, capacity, location, destination):
self.bus_name = name
self.bus_capacity = capacity
self.bus_location = location
self.bus_destination = destination
self.seats = []
self.people = []
def Bus_properties(self):
print(self.bus_name + ", " + str(self.bus_capacity) + ", " + self.bus_location + ", " + self.bus_destination)
def print_list(self):
a = self.people
print(self.people)
def get_route(self):
return self.bus_location + self.bus_destination
def get_on(self, Person):
if len(self.people) < 20: #Loop through all the seats
self.people.append(Person)
else:
print('fulll')
def get_name(self):
print(self.name)
import People
import Transport
C2C = Transport.Bus("C2C", 30, "Ithaca", "New York")
Fred = People.Person("Fred", 12323, 13, "Ithaca", "New York")
Jennifer = People.Person("Jennifer", 111117, 56, "Ithaca", "New York")
Fred.add2bus(C2C)
Jennifer.add2bus(C2C)
我想创建一个 while 循环,它采用 peoplelist 的长度和条件 while x < len(C2C.people) 然后它将该公共汽车上的所有人员姓名附加到列表 y
像这样...
x = 0
y = []
while x < len(C2C.people) :
y.append((C2C.people[x].get_name()))
x = x + 1
print(y)
但我得到了这个结果:Fred [None] Jennifer [None, None]