1

I need to program a Monopoly game for my high school programming class. This is part of the code I'm working on then I'm going to implement it into the rest of my code. I'm expecting it to ask the input (brownhouse1), but when I run it I get nothing for the output. There's probably something really simple wrong with it but I can't figure out what's causing it to output nothing.

I've tried messing with the operators, changing the house values and it will output nothing, I'm not sure what I need to change. (Also sorry for the bad formatting, first time using stackoverflow.)

Thank you!

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
3
  • What output do you expect?
    – gmds
    Commented May 16, 2019 at 1:25
  • I'm expecting the first two if statements to be true and then for it to ask "Would you like to buy a house?"
    – Maddie G
    Commented May 16, 2019 at 1:26
  • 1
    Possible duplicate of How to test multiple variables against a value?
    – user9679969
    Commented May 16, 2019 at 1:49

2 Answers 2

1

If you find yourself changing things just to see if anything different happens, it's often an indicator that your approach has some kind of problem, and backing up to take a wider view can help.

In this case, you're tracking several different values for the owner, when you'd probably be better off with a single object that can handle its own name, etc., should you need those things.

I think you're a bit confused on how comparison logic statements work. Your first if statement tests for the existence of MeditaranianAve.ownername and then whether BalticAve.ownername equals "Player2". If you want to check whether they both equal that value:

if MeditaranianAve.ownername == BalticAve.ownername == "Player2":
    # ...

You can clean this up a lot, though, by tracking more of your state with objects that can have their own methods. That way you can put logic relevant to a particular type in its own place.

This might be a place to start. It adds some relational logic that might be unfamiliar, but walking through it will show you a common approach to related data models and separation of concerns for data types in 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.')
2
  • Wow! Thank you so much! I really appreciate you taking your time to help me! :)
    – Maddie G
    Commented May 16, 2019 at 2:24
  • You're welcome! I hope it introduces you to some useful concepts. If it helped, please consider accepting the answer. :)
    – kungphu
    Commented May 20, 2019 at 1:34
0

The problem is that MeditaranianAve.house and BalticAve.house <= 4 evaluates to 0

Just do MeditaranianAve.house <= 4 and BalticAve.house <= 4 instead

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.