我正在创建这个程序,它的一个函数是输出一个在构造函数中初始化的列表。但是,发生的事情是它以十六进制或其他形式输出内存位置,我不知道为什么。
我有两个班和一个跑步班: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)的条件,然后将总线上所有人的名字附加到一个list y
像这样。。。x = 0
y = []
while x < len(C2C.people) :
y.append((C2C.people[x].get_name()))
x = x + 1
print(y)
但我得到的结果是:
弗莱德
[无]
詹妮弗
[无,无]