fork download
  1. def inside(command, state):
  2. if "yes" in command:
  3. state["won"] = True
  4. elif "no" in command:
  5. state["hp"] = -100
  6. else:
  7. print("Unknown command")
  8.  
  9. def outside(command, state):
  10. if "use" in command and "key" in command:
  11. if state.get("has_key") == True:
  12. print ("You use the key and enter inside the building.")
  13. print ("Now that you are inside, do you want to win?")
  14. state["location"] = inside
  15. else:
  16. print ("You do not have a key and you get shocked for trying (-1hp)")
  17. state["hp"] -= 1
  18. elif "look" in command:
  19. if "key" in command:
  20. print ("The key is nice and shiny")
  21. elif "door" in command:
  22. print ("The door is nice and strong")
  23. else:
  24. print ("You are standing outside. There is a key and a door")
  25. elif ("get" in command or "pick" in command) and "key" in command:
  26. state["has_key"] = True
  27. print("You got the key")
  28. else:
  29. print ("Unknown command")
  30.  
  31. def intro(command, state):
  32. print("Welcome to the text adventure tutorial. You are standing outside. There is a key and a door")
  33. state["location"] = outside
  34.  
  35.  
  36. state = {"hp": 3, "location": intro, "won": False}
  37. command = ""
  38.  
  39. state["location"](command, state)
  40. while state["hp"] > 0 and state["won"] == False:
  41. command = input("Command? ").lower()
  42. state["location"](command, state)
  43.  
  44. if state["hp"] <= 0:
  45. print ("You died.")
  46. elif state["won"] == True:
  47. print ("You win! (hp:", state["hp"],")")
Success #stdin #stdout 0.02s 9328KB
stdin
look at key
use key on door
get key
use key on door
yes
stdout
Welcome to the text adventure tutorial. You are standing outside. There is a key and a door
Command? The key is nice and shiny
Command? You do not have a key and you get shocked for trying (-1hp)
Command? You got the key
Command? You use the key and enter inside the building.
Now that you are inside, do you want to win?
Command? You win! (hp: 2 )