我需要为我的高中编程班编写一个Monopoly游戏。这是我正在处理的代码的一部分,然后将其实现到其余代码中。我希望它能询问输入(brownhouse1),但是当我运行它时,输出什么也没有。可能有一个非常简单的错误,但是我不知道是什么原因导致它什么也不输出。
我尝试与操作员搞混,更改房屋价值,但不会输出任何东西,我不确定需要更改什么。 (也为第一次使用stackoverflow的格式错误而感到抱歉。)
谢谢!
class Properties:
def __init__(self,name,qprice,qrent,owner,ownername,color,house):
self.name = name
self.price = int(qprice)
self.rent = int(qrent)
self.owner = owner
self.ownername = ownername
self.color = color
self.house = int(house)
MeditaranianAve = Properties("MeditaranianAve",60,2,"Yes","Player2","Brown",0)
BalticAve = Properties("Baltic Ave",60,4,"Yes","Player2","Brown",1)
Brown = (MeditaranianAve, BalticAve)
if MeditaranianAve.ownername and BalticAve.ownername == "Player2":
if MeditaranianAve.house and BalticAve.house <= 4:
brownhouse1 = input("Would you like to buy a house? ")
if brownhouse1 == "y":
if BalticAve.house > MeditaranianAve.house:
Med1 = input("You own more houses on Baltic Ave than Meditaranian Ave, you can only purchase a house for Meditaranian Ave, would you like to purchase? y/n?")
if Med1 == "y":
MeditaranianAve.house = MeditaranianAve.house+1
if MeditaranianAve.house > BalticAve.house:
Balt1 = input("You own more houses on Meditaranian Ave than Baltic Ave, you can only purchase a house for Baltic Ave, would you like to purchase? y/n?")
if Balt1 == "y":
BalticAve.house = BalticAve.house+1
答案 0 :(得分:1)
如果您发现自己只是为了看看是否有任何不同的事情发生变化而进行更改,这通常表明您的方法存在某种问题,并且进行更广泛的备份可以有所帮助。
在这种情况下,您正在为所有者跟踪几个不同的值,如果您可能需要一个可以处理其自己名称的对象,那么可能会更好。
我认为您对比较逻辑语句的工作方式有些困惑。您的第一个if
语句测试MeditaranianAve.ownername
的 existence ,然后测试BalticAve.ownername
是否等于“ Player2”。如果要检查它们是否都等于该值:
if MeditaranianAve.ownername == BalticAve.ownername == "Player2":
# ...
但是,通过使用可以拥有自己方法的对象跟踪更多状态,您可以进行很多清理。这样,您可以将与特定类型相关的逻辑放在自己的位置。
这可能是一个起点。它添加了一些可能不熟悉的关系逻辑,但是逐步学习将向您展示一种用于处理相关数据模型和分离Python中数据类型关注点的通用方法。
class PropertyGroup(object):
def __init__(self, name):
self.name = name
self.properties = []
def fully_owned_by(self, player):
for property in self.properties:
if property.owner != player:
return False
return True
class Property(object):
def __init__(self, name, group):
self.name = name
# This assumes all properties are part of a group; otherwise,
# default to None and check whether this exists before trying
# to access it.
self.group = group
group.properties.append(self)
self.owner = None
self.house_count = 0
def houses_available(self):
return self.house_count < 5
def purchase_house(self):
# This is where you'd check to make sure the owner exists and
# can purchase a house, then implement that logic.
pass
class Player(object):
def __init__(self, name):
self.name = name
player = Player('Maddie')
group = PropertyGroup('B&M')
baltic = Property('Baltic Avenue', group)
mediterranean = Property('Mediterranean Avenue', group)
baltic.owner = player
# Since Baltic but not Mediterranean is owned by the player, this
# check will be False.
if baltic.group.fully_owned_by(player) and baltic.houses_available():
print('Prompt for house purchase')
else:
print('No dice.')
mediterranean.owner = player
# Now that both are player-owned, this is True. Note that baltic.group,
# mediterranean.group, and our local variable group all reference the
# same group object, so you could call that method on any of them here.
if mediterranean.group.fully_owned_by(player) and mediterranean.houses_available():
print('Prompt for house purchase')
else:
print('No dice.')
答案 1 :(得分:0)
问题是MeditaranianAve.house and BalticAve.house <= 4
的评估结果为0
只需MeditaranianAve.house <= 4 and BalticAve.house <= 4