部分学校作业没有输出任何内容(初学者,Python)

2024-09-29 19:36:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要为我的高中编程课编写一个垄断游戏。这是我正在编写的代码的一部分,然后我将把它实现到我的其余代码中。我希望它询问输入(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

Tags: nameselfyouifpropertiespurchasecolorhouse
2条回答

问题是MeditaranianAve.house and BalticAve.house <= 4的计算结果是0

只要做MeditaranianAve.house <= 4 and BalticAve.house <= 4就行了

如果你发现自己在改变一些事情,只是为了看看是否有什么不同的事情发生,这往往是一个指标,表明你的方法有某种问题,而备份,以采取更广泛的观点可以帮助。你知道吗

在本例中,您正在为所有者跟踪几个不同的值,如果您需要这些东西,最好使用一个可以处理自己名称等的对象。你知道吗

我觉得你对比较逻辑语句的工作原理有点困惑。你的第一个if语句测试MeditaranianAve.ownername的存在,然后测试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.')

相关问题 更多 >

    热门问题