我似乎在将对象附加到其他对象列表时遇到麻烦。 尽我所能解释:
我是python的新手,我试图学习类和对象,所以我为自己准备了一个小exe
我的程序中有3个对象:投资者,房屋和市场
在Market课堂上,我仅有的是房屋清单
我有一个由3个投资者组成的列表,每个投资者都有一个作为房屋列表的字段(在第一个 init 处开始为空)
现在,我要做的是显示市场上的房屋清单(我已经完成了) 用户输入要购买的房屋的索引,然后程序会将房屋添加到其房屋列表中,并将其从市场中的房屋列表中删除。
现在我的问题似乎是,每当我选择一所房子时,它就会将其添加到所有3个投资者中,而不是添加到特定的投资者中,我不知道为什么 (可能是因为它是ref类型,但我不知道如何克服它:/)
代码:
class Investor:
def __init__(self, name, budget, houses):
self.name = name
self.budget = budget
self.houses = houses
def BuyHouse(self, market, houseIndex):
self.budget -= market.availableHouses(houseIndex).price
self.houses.append(market.availableHouses[houseIndex]) ## i believe this to be the problem.
print(f'Budget left: {self.budget}$')
def PrintAllInfo(self): # prints all info for houses.
counter = 1
for h in self.houses:
print(f'{counter}) The House is in: {h.location}\n The House price is: {h.price}$\n The House size is: {h.size} sqft')
counter += 1
class House:
def __init__(self, location, price, length, width):
self.location = location
self.price = price
self.width = width
self.length = length
def PrintThisHouse(self):
print(f'{self.location}, Price - {self.price}$')
class Market:
def __init__(self, availableHouses):
self.availableHouses = availableHouses
def PrintAllHouses(self):
print('\nALL ABAILABLE PROPERTIES:\n--------------------------')
counter = 1
for h in self.availableHouses:
print(str(counter) + ' - ', end='')
counter += 1
h.PrintThisHouse()
# main #
invHouses = []
investors = [ Investor('Bot1', 100000000, invHouses),
Investor('Bot2', 100000000, invHouses),
Investor('Bot3', 100000000, invHouses) ]
# init Houses:
houses = [ House("LA, California",450000,20,10),
House("IL, Tel Aviv",1000000,5,5),
House("Rus, Moscow",40000,20,10),
House("Switz, Zr",125000,7,9),
House("Fr, Paris",225000,15,4),
House("Eg, Cairo",75000,15,15),
House("Pt, Lisbon",100000,10,10),
House("Ge, Batumi",75000,14,6),
House("In, New Delhi",50000,20,20),
House("Ca, Montreal",500000,30,35),
House("Cambodia, Phnom Pen",15000,9,9),
House("Uk, London",1000000,10,10) ]
#init Market:
mrkt = Market(houses)
# runs 3 times 1 time for each investor to buy one house.
for inv in investors: # run for all investors
mrkt.PrintAllHouses() # a func that prints all the houses that are on the market
selectHouse = (int(input('Enter the house you want to buy: ')) - 1)
inv.BuyHouse(mrkt, selectHouse)
#after each investor bought a house i want to print for each investor his house(s) in the list
# problem is that now it prints all 3 houses for each one and i dont know why :(
for inv in investors:
print(f'\nProperties of {inv.name}:')
inv.PrintAllInfo()
print('\n**************************')
非常感谢您的帮助
答案 0 :(得分:3)
invHouses = []
investors = [ Investor('Bot1', 100000000, invHouses),
Investor('Bot2', 100000000, invHouses),
Investor('Bot3', 100000000, invHouses) ]
您有一个由invHouses
创建的列表对象。您会将同一对象传递给所有投资者。因此,如果您通过一位投资者将房屋添加到该列表中,那么所有其他投资者也会将该房屋添加到他们的列表中(因为它是同一列表)。
investors = [ Investor('Bot1', 100000000, []),
Investor('Bot2', 100000000, []),
Investor('Bot3', 100000000, []) ]
将不同的对象传递给每个投资者。
答案 1 :(得分:0)
我认为问题在于您正在将相同的清单invHouses传递给所有投资者。尝试将self.houses初始化为投资者初始状态中的空列表