Created
May 16, 2012 14:04
-
-
Save Lysander/2710568 to your computer and use it in GitHub Desktop.
Sample code fordiscussion in: http://www.python-forum.de/viewtopic.php?f=1&t=29274
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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