0

So I'm trying to make a text adventure game in Python 2.7 with limited options. I want to find a way where if an answer that isn't programmed in is input, it will re-ask the question. Thanks.

while True:  
print "You wake up in a dim room. Cold water trickles down your spine through the rags you are wearing." 
  break
while True:
  q1 = raw_input("Exit or Explore? ")
  if q1 in ("explore", "Explore","EXPLORE",):
    print "You search the room. All you find is a small, maroon object. You reach for it, but all of the sudden, it explodes. You are dead."
    exit()
  elif q1 in ("exit","EXIT","Exit",):
    print "Light flashes before your eyes as you find yourself on a scenic mountaintop. It drops to the north and west. To the east is a winding road. In the distance you see a village."
  else:
    print "..."
  q2 = raw_input("Which direction do you go? ")
  if q2 in ("North","north","NORTH","West","west","WEST",):
    print "You clumsily stumble off the cliff. You are dead."
    exit()
  elif q2 in ("east","EAST","East",):
    print "You travel for a few hours and arrive at"
    break
1
  • Just a side note, but the first while loop you have isn’t necessary. You can just print out that text. Commented Jan 26, 2019 at 8:56

1 Answer 1

2

There is a feature implemented in most programming languages called a method or function. Basically what a function does is when you call it, you also call any instructions you defined in that function. Oh yeah and just for the record method and function are practically the same. So in your case you could have something like:

def exit_or_explore():
    q1 = raw_input("Exit or Explore? ")

    if q1 in ("explore", "Explore","EXPLORE",):
        print "You search the room. All you find is a small, maroon object. You reach for it, but all of the sudden, it explodes. You are dead."
        exit()
    elif q1 in ("exit","EXIT","Exit",):
        print "Light flashes before your eyes as you find yourself on a scenic mountaintop. It drops to the north and west. To the east is a winding road. In the distance you see a village."
    else:
        exit_or_explore()
exit_or_explore()

You define the function with keyword def and whenever you want to call anything in the code you call it like function() What happens is if the user doesn't respond with any of your words listed it calls the function exit_or_exlore() which repeats the code inside it.

Next time please format your code properly

Sign up to request clarification or add additional context in comments.

1 Comment

If this post helped you please consider marking it as an anwser.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.