Skip to content

Instantly share code, notes, and snippets.

@Lysander
Created May 16, 2012 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lysander/2710568 to your computer and use it in GitHub Desktop.
Save Lysander/2710568 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
ITEMS = {
'Werkzeug': 20,
'Öl': 4,
'Holzbalken': 6,
'Korb': 3,
'Stoff': 8,
'Kreide': 1,
'Seil': 4,
'Holzeimer':7,
'Harke': 5,
'Axt': 8,
'Lederbeutel': 3
}
class Shop:
def __init__(self, items):
self.items = items
def __str__(self):
return "\nShop:\n{}".format("\n".join("{}: {}".format(name, price) for
name, price in self.items.items()))
def append(self, name, price):
self.items[name] = price
def sell(self, name):
return self.items.pop(name)
class Player:
def __init__(self, name):
self.name = name
self.items = {}
def __str__(self):
return "\n{}:\n{}".format(self.name,
"\n".join("{}: {}".format(name, price) for
name, price in self.items.items()))
def buy(self, name, price):
self.items[name] = price
def handle_purchasing(name, player, shop):
price = shop.sell(name)
player.buy(name, price)
def main():
p = Player("Hyperion")
print(p)
s = Shop(ITEMS)
s.append("Python", 42)
print(s)
for name in ("Öl", "Seil", "Axt"):
handle_purchasing(name, p, s)
print(s)
print(p)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment