from sys import exit def start(): print "\nWelcome. It's time to play a game!" print "You are standing in a meadow." print "You see a farmhouse and a mountain path." print "Where do you go?" def meadow(): next = raw_input("> ") if "farmhouse" in next: farmhouse() elif "mountain path" in next: mountain_path() else: print "\nYou are lost. Choose \'farmhouse\' or \'mountain path.\'" meadow() def mountain_path(): print "\nThe mountain path brings you to a stream." print "How do you cross it?" cross_stream() def cross_stream(): next = raw_input("> ") if "swim" in next: end("\nYou don't know how to swim, and you drown.") elif "use the bridge" in next: two_roads_diverge() else: print "\nYou can't do that! Either \'swim\' or \'use the bridge.\'" cross_stream() def two_roads_diverge(): print "\nYou crossed the stream!" print "Now, two roads diverge in a yellow wood." print "And you are sorry you cannot travel both." print "Do you choose the \'road less traveled\' or the \'road more traveled.\'" next = raw_input("> ") if "road less traveled" in next: road_less_traveled() elif "road more traveled" in next: road_more_traveled() else: end("You go nowhere, and a bear finds you. That's a problem.") def road_less_traveled(): print "\nYou found a pot of gold!" print "But it is guarded by a leprechaun." print "Do you fight the leprechaun or trick the leprechaun?" while True: next = raw_input("> ") if next == "fight the leprechaun" or next == "fight": end("\nYou win the fight and get the gold. Leprechauns are tiny.") elif next == "trick the leprechaun" or next == "trick": end("\nYou are tricky but leprechauns are trickier! No gold for you!") else: end("\nThat didn't work. Good try though!") def road_more_traveled(): print "\nThe road brings you to a city." print "You need a job." print "The city needs bankers, bakers, and craftsmen." print "Which do you choose?" next = raw_input("> ") if "banker" in next: banker() elif "craftsmen" in next or "craftsman" in next: craftsmen() elif "baker" in next: baker() else: end("\nYou fail to find a job and wander the streets aimlessly.") def banker(): print "\nYou have been selected to become a time traveling banker!" print "Do you want to travel time?" next = raw_input("> ") if "yes" in next: end("""\nYou travel to 2008. \nThe financial crisis hits. Because you disrupted the space-time continuum, no government bailout happens. You lose.""") elif "no" in next: end("\nYou become fabulously wealthy and buy a lot of sheep. Good job!") else: print "\nYou can't do that. You must choose." banker() def craftsmen(): print "\nYou make the finest silk west of the Tibetan Plateau." print "How much do you sell it for?" while True: try: next = float(raw_input("> $")) break except ValueError: print "Oops! That was no valid number. Try again..." if next <= 60.00: print "You sell thousands of pieces of silk." end("\nYou retire after 20 years and spend the rest of your days on a fishing boat in the Red Sea. Good job!") else: end("\nYour greed is your downfall. \nNo one will buy your silk at that price. Oh well.") def baker(): print "\nYou are now a baker!" print "There is a terrible bread shortage in the city!" print "Only you can bake enough bread to feed the hundreds of people." print "How many loaves will you bake?" while True: try: next = float(raw_input("> ")) break except ValueError: print "You must pick a number. Use digits only, and try again!" if next <= 100: print "\nThat will not feed the city, but you will need these ingredients anyway:" elif next > 100: print "\nThat will feed the city!" print "You will need these ingredients:" else: end("\nI guess you just want everyone to starve then.") eggs = next * 3 flour = next * 2.5 water = next * 1.5 yeast = next / 4 print "\nYou will need %r eggs." % eggs print "You will need %r cups of flour and % r cups of water." % (flour, water) print "You will also need %r scoops of yeast." % yeast end("\nHappy baking!") def farmhouse(): print "\nYou enter a courtyard." print "There is a farmhouse and a barn." print "Where do you go?" next = raw_input("> ") if next == "farmhouse": inside() elif next == "barn": barn() else: end("\nA giant turkey chases you away. Goodbye!") def inside(): print """\nYou enter a quaint farmhouse. It has a basement and an attic. Which will you explore?""" next = raw_input("> ") if next == "attic": end("""\nThere are ghosts in the attic. Run! Too late. You are now a ghost.""") elif next == "basement": end("\nYou find a rare artifact. Good job!") else: end("\nAn earthquake knocks the house down while you are inside. The end.") def barn(): print "\nThere are many animals in the barn." animals = ['horses', 'chickens', 'cows', 'pigs', 'goats'] for animal in animals: print "There are %s." % animal print "There is also a lot of hay." print "You are tired and don't want to walk anymore." print "Do you steal a horse, take a nap in the hay, or fry some eggs?" next = raw_input("> ") if next == "steal a horse" or next == "steal horses" or next == "horse": end("\nYou ride off into the sunset never to be seen again.") elif next == "nap" or next == "take a nap": end("\nYou wake rested and continue your journey another day.") elif next == "eggs" or next == "fry some eggs": print "\nYou don't have a stove." print "You light the hay on fire for a heat source." end("The barn burns down, and you run away, hungry.") else: end("\nYou pass out and are eaten by a werewolf. You probably should have napped.") def end(why): print "\n" print why print "\n----------" print "\nThe game is over" print "\n----------" print "\nDo you want to try again?" next = raw_input("> ") if next == "yes": start() meadow() elif next == "no": exit(0) else: exit(0) start() meadow()
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