0

Firstly, sorry for the long title.

So... I am making a text adventure game in Python for learning purposes. I spent most of last night trying to squash a bug in my game where no matter what you typed it would take you 'north'. I could input "sadjbfl" and it would just take me "north" instead of giving an error. (But I could also put "south" and it would take me south...)

I figured out a "while True" loop by searching for how to do this online but then I ran into another couple of problems with it, the first being that I could not make it accept multiple inputs. I could not allow it to accept "'north' OR 'n'"; it would then just accept literally anything as the first choice. The second problem is that I believe I would need a "while True" loop for every choice in the game.

The other way I have tried to accomplish this is a simple "ask for 'north' or 'n' and if it's none of that then print 'error'" but when it prints an error the program stops running when I need it to re-ask the question.

"Solution" 1:

# this code works perfectly fine
# if you don't type what it wants
# it keeps asking the question again

while True:

    user_input = input("Enter 'test': ")


    if user_input.lower() == "test":
        print("\nCorrect input!")
        break

    else:
        print ("\nInvalid input!")

"Solution" 1b (problem):

while True:

    user_input = input("Enter 'test': ")

    # With '"test" or "t"', it allows ANY input;
    # it does not give an error and just prints
    # 'Correct!'.
    if user_input.lower() == "test" or "t":
        print("\nCorrect input!")
        break

    else:
        print ("\nInvalid input!")

"Solution" 2:

# "ch" means "choice".
# (naming scheme for less space)
ch_1 = input("\nGo north or south? ")

if ch_1.lower() in ["north", "n"]:
    print("\nYou went north!")

elif ch_1.lower() in ["south", "s"]:
    print("\nYou went south!")

# if this 'else' prints it ends the program instead of re-asking
# the question of 'which way to go?'
else:
    print("I don't know what that means.")

EDIT: Nothing I've read on this site has actually helped me concerning this problem, not even the "duplicate" of this one (which I read before posting this).

My problem:

When I use the "while True" loop, my code becomes incredibly restrictive.

Every single choice using this method needs to be wrapped in an "while True" statement and it limits my options severely. It works perfectly if I only have one choice/path in that block BUT as soon as I add another choice/path, the new path experiences the same exact problem that I made this post for to begin with.

If there's some way to just always spit out a predefined message saying "ERROR! Check for misspellings!" or whatever every time the user does not put in what I want them to then that would be fantastic. (The "duplicate" of this post had something like this (I think) but it didn't work. /shrug)

But in the meantime I just cannot figure this out. Maybe I should just go back to the basics for a while.

  • You're misding indentations. So nothing happens in your while-block. – RoyM Nov 26 '18 at 21:47
  • Formatting mistake, RoyM. I have indentations in my code. On this site I need to put 4 spaces for it to recognize it as code and 4 more for the indentation to show up. My mistake. –  Nov 26 '18 at 21:50

1 Answers1

0

In solution 2 only the loop is missing:

while True:
    # name your variables correctly and no comment is needed.
    choice = input("\nGo north or south? ")

    if choice.lower() in ["north", "n"]:
        print("\nYou went north!")
        break
    elif choice.lower() in ["south", "s"]:
        print("\nYou went south!")
        break
    else:
        print("I don't know what that means.")

You can write a function to reuse the loop:

def ask(question, answers):
    while True:
        choice = input(question).lower()
        if choice in answers:
            break
        print("I don't know what that means.")
    return choice

choice = ask("\nGo north or south? ", ["north", "n", "south", "s"])
if choice[0] == "n":
    print("\nYou went north!")
else: # the only other choice is "south"
    print("\nYou went south!")
Daniel
  • 42,087
  • 4
  • 55
  • 81
  • So do I need a loop for every question throughout the entire game? I was hoping to avoid that much repetition. –  Nov 26 '18 at 21:46
  • 1
    @Sola Yes, you need a loop for every question, but you can take care of the repetition by encapsulating everything in a function and calling that one passing the valid responses as parameters and returning the user input. – ChatterOne Nov 27 '18 at 09:16
  • @Daniel I think you forgot a `return choice` at the end of the function – ChatterOne Nov 27 '18 at 14:37
  • @ChatterOne: you're right. – Daniel Nov 28 '18 at 19:58