#------------------------------------------------ # 'Crafting Challenge' Game # More programs at UsingPython.com/programs #------------------------------------------------ #------------------------------------------------ # Challenge 1: Can you craft a tent and a firepit? # Challenge 2: Can you add more items? # Challenge 3: Can you create crafting rules # to make more items? # Challenge 4: Add comments to the code below! #------------------------------------------------ commands = { "i" : "see inventory", "c" : "see crafting options", "craft [item]" : "craft something from inventory items", } #an inventory of items items = { "flint" : 50, "grass" : 100, "hay" : 0, "tree" : 100, "log" : 0, "sapling" : 100, "twig" : 0, "boulder" : 30, "rock" : 0, "pickaxe" : 0, "axe" : 0, "firepit" : 0, "tent" : 0, "torch" : 0, } #rules to make new objects craft = { "hay" : { "grass" : 1 }, "twig" : { "sapling" : 1 }, "log" : { "axe" : 1, "tree" : 1 }, "axe" : { "twig" : 3, "flint" : 1 }, "tent" : { "twig" : 10, "hay" : 15 }, "firepit" : { "boulder" : 5, "log" : 3, "twig" : 1, "torch" : 1 }, "torch" : { "flint" : 1, "grass" : 1, "twig" : 1 }, "pickaxe" : { "flint" : 2, "twig" : 1 } } print("'Crafting Challenge' Game") print("More programs at UsingPython.com/programs") print("-----------------------------------------\n") print("TRY TO SURVIVE BY CRAFTING A TENT AND A FIREPIT!") print("type '?' for help") while True: command = input(">").split() if len(command) == 0: continue if len(command) > 0: verb = command[0].lower() if len(command) > 1: item = command[1].lower() if verb == "?": for key in commands: print(key + " : " + commands[key]) print("\n") elif verb == "i": for key in items: print(key + " : " + str(items[key])) print("\n") elif verb == "c": for key in craft: print(key + " can be made with:") for i in craft[key]: print(str(craft[key][i]) + " " + i) print("\n") elif verb == "craft": print("making " + item + ":") if item in craft: for i in craft[item]: print(" you need : " + str(craft[item][i]) + " " + i + " and you have " + str(items[i])) canBeMade = True for i in craft[item]: if craft[item][i] > items[i]: print("item cannot be crafted\n") canBeMade = False break if canBeMade == True: for i in craft[item]: items[i] -= craft[item][i] items[item] += 1 print("item crafted\n") if items["tent"] >= 1 and items["firepit"] >= 1: print("\n**YOU HAVE MANAGED TO SURVIVE!\nWELL DONE!") break else: print("you can't") else: print("you can't")
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question