pythoneventsinputtuplesadventure

Print the correct event, depending on user's input


I am working on a text-based adventure game. Essentially I want to return the correct event depending on what the user types. Right now, it gives the same event no matter what the user types (EVENT_DRAGON is returned every time). The majority of the game, the user had choices between 1, 2, or 3. Which works perfectly fine and well, but I wanted to switch it up and ask the user for a word input. This hasn't been working correctly.. Clarification on why it would work with numbered input but not word input would be appreciated. Thank you.

def main():
    import sys


    def run_event(event):
        text, choices = event
        text_lines = text.split("\n")
        for line in text_lines:
            print('' + line)
            print("")
        choices = choices.strip("\n")
        choices_lines = choices.split("\n")
        for num, line in enumerate(choices_lines):
            print('' + line)
            print("")

    print ("")
    print ("You have found yourself stuck within a dark room, inside this room are 5 doors.. Your only way out..")
    print ("")
    print ("Do you want to enter door 1,2,3,4, or 5?")
    print ("")


    EVENT_DOOR3 = ("""
    A dragon awaits you
    ""","""
    'Welcome' remarks the dragon. 'Do you wish to escape?""")

    EVENT_DRAGON = ("""
    'Good choice' says the dragon. Where would you like to escape to?
    ""","""
    1. Just get me out of here!
    2. Where should I escape to?
    """)

    EVENT_DRAGON2 = ("""
    Interesting..
    ""","""
    Test..
    """)

    door = input("> ")
    if door == "3":
      run_event(EVENT_DOOR3)
      dragon = input()
      if dragon in ['yes','Yes']:
        run_event(EVENT_DRAGON)
      elif dragon in ['no','No']:
        run_event(EVENT_DRAGON2)


main()

Solution

  • This line is going to give you some trouble because it always evaluate to True.

    if dragon == "yes" or "Yes":
        run_event(EVENT_DRAGON)
    

    This condition is similar to saying:

    if (dragon == 'yes') or ('Yes'):
        run_event(EVENT_DRAGON)
    

    Since 'Yes' is a non-empty string it will evaluate to True and it will always do run_event(EVENT_DRAGON). There are a couple different ways we could fix this. First you could change the input to lowercase to only evaluate one word:

    if dragon.lower() == 'yes':
        run_event(EVENT_DRAGON)
    

    Additionally, you could put the acceptable words in a list:

    if dragon in ['Yes', 'yes']:
        run_event(EVENT_DRAGON)
    

    Also you could test each word individually:

    if dragon == 'yes' or dragon == 'Yes':
        run_event(EVENT_DRAGON)
    

    Hopefully that helps. Let me know if that doesn't work.