# An example choose your own adventure game # Please take ideas from this one but DO NOT START FROM THIS FILE # You will need to plan out your story before starting from random import * from time import * def show_intro(): print("You have just arrived at a lakeside restaurant with your friends.") print("It turns out this restaurant rents kayaks!") print("Your friend Bob is very hungry but you really want to go kayaking.") def choose_activity(): activity = "" while(activity != "eat" and activity != "kayak"): # this makes sure the user types in valid input activity = raw_input("What are you going to do? (eat or kayak)") return activity # send back the activity to where the choose_activity function was called def do_activity(activity): global money, alive # the global keyword allows us to use variables defined outside the function if(activity == "kayak"): print("You pay $20 for the kayak rental and get in.") money = money - 20 sleep(1) # sleep causes the computer to pause for a bit print("Your friend Bob tosses you a life jacket.") sleep(1) print("You put on your sunglasses.") sleep(2) tragedy = randint(1, 2) # events can be randomly chosen this way if(tragedy == 1): print("A horrible sea creature jumps out of the water!") print("Your friend Bob swats it with a paddle just as it opens its mouth...") sleep(2) print("You have been eaten.") alive = False else: print("You pleasantly kayak into the sunset.") else: print("You wait 20 minutes to get seated.") sleep(1) ripoff = randint(1, 2) if(ripoff == 1): print("You complete your meal.") sleep(2) print("The waiter comes back with your bill -- $60!!") sleep(1) print("You don't have enough money, so you have to wash dishes.") money = money - 40 sleep(1) print("In the end, you pay $40.") sleep(2) else: print("Yum.") sleep(1) print("That will be $20.") money = money - 20 sleep(2) def go_shopping(): global money print("You go shopping.") price = 1000 # initialize price so we go into the while loop item = "" # initialize item so we can use it after the while loop while(price > money): item = raw_input("What do you want to buy? ") price = randint(10, 80) if(price > money): print("Sorry, you can't afford that -- it is $" + str(price) + " and you have $" + str(money)) print("That " + item + " will be $" + str(price) + ".") money = money - price print("You now have $" + str(money)) play = "yes" money = 0 alive = True # keeps track of whether you have died # Example of a boolean flag: can be either True or False while play == "yes" or play == "y": # You can get here in the code either because it's the first time you're playing # or because you are playing a new round so you need to reset your variables money = 50 # Reset money for the next round alive = True # Reset alive for next round show_intro() activity = choose_activity() # choose_activity returns a value, so we need to save it do_activity(activity) if(alive == True): go_shopping() play = raw_input("Do you want to play again? (yes or no)") print("You quit. You have $" + str(money) + " left.")