0

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()

1 Answers1

0

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.

Smidem
  • 42
  • 7
  • Thank you! Yes that helped, BUT for some reason it doesn't run the "no" event.. would it be any different for an ELIF statement?? @Smidem – Idealprinciple Aug 28 '18 at 16:16
  • It shouldn't work differently for the ELIF statement, but I did notice your elif is indented strangely. Whitespace matters in Python, so I would double check that all the statements are at the same indentation level (remove one space before `run_event(EVENT_DRAGON2)`). – Smidem Aug 28 '18 at 16:27
  • I caught that as well, but even after I properly removed the space before, it's still not running the event.. I'm unsure why @Smidem – Idealprinciple Aug 28 '18 at 16:31
  • Did you implement a similar approach for the ELIF statement? For instance, `elif dragon in ['No', 'no]:`. Otherwise it will not catch a lowercase no. – Smidem Aug 28 '18 at 16:34
  • I did, I edited my example from above to reflect exactly how I have it. Still wont run the EVENT_DRAGON2 for some reason @Smidem – Idealprinciple Aug 28 '18 at 16:40
  • You know, I'm not quite sure why this is happening, but does it work if you use no with quotes? Like when it prompts you for input does `'no'` produce different results than just `no`? – Smidem Aug 28 '18 at 16:52
  • Hmm, I'll give that a shot when I get back to my laptop. Not sure why'd that work though, since the first if statement has ' '. I'll let you know here in a bit @Smidem – Idealprinciple Aug 28 '18 at 17:04
  • @Idealprinciple If that does turn out to be the case, it could be an issue with Python 2 vs. Python 3. If you are using Python 2 you may need to change `input()` to `raw_input()`. I found this [question](https://stackoverflow.com/questions/16175809/python-removing-required-quotes-from-input) to be helpful. – Smidem Aug 28 '18 at 17:37
  • Thank you for your help! I got it to work, I just re-indented the whole thing and it's going now. Thank you again! – Idealprinciple Aug 28 '18 at 18:41
  • @Idealprinciple No problem! Glad I could held! – Smidem Aug 28 '18 at 19:42