0

I am making a choose your own adventure game and I have a function to check if what you input in the console is acceptable or not. In the beginning, you can only type "turn on light" if you type anything else it will return as an error and you will be prompted to type an actual action. The problem I have is after you type something that isn't accepted it will not let you continue after you make an error.

actions = ['help','turn light on',]

def errorcheck(player_input):
    if player_input in actions:
        error = False
        return()
    else:
        error = True
        while error == True:
            print('i dont know what you mean by',player_input)

            player_input = input('>')

            if player_input in actions:
                error = False
            else:
                error = True

print('welcome to TITLE')
print('type help at anytime to see your options')
print('">" that symbol promts you to do something')
print('')
print('you wake up, its dark')



player_input = input('>')

errorcheck(player_input)

if error == False:
    if player_input == ('help'):
        playerhelp = True
        while playerhelp == True:
            print('you can: turn light on')
            playerhelp = False
  • Possible duplicate of [python function modifying variable in calling scope](https://stackoverflow.com/questions/51195343/python-function-modifying-variable-in-calling-scope) – MisterMiyagi Jan 25 '19 at 15:49

2 Answers2

1

errorcheck potentially modifies player_input that it accepts as an argument. It is a new local variable that has nothing to do with the global player_input.

A naive solution would be to make player_input a global variable, but that would be a bad, anti-pattern solution for several reasons:

  • global variables tend to lead to messy, hard to debug code
  • a function should preferably do one thing, and that thing should preferably be what its name suggests it does.

Instead, have errorcheck only check the input as its name suggests.

def errorcheck(player_input):
    return player_input not in actions

player_input = None

while errorcheck(player_input):
    player_input = input('>')

At this point having errorcheck as a function seems a bit superfluous. You do not really need it:

player_input = None

while player_input not in actions:
    player_input = input('>')
DeepSpace
  • 78,697
  • 11
  • 109
  • 154
-1

First of all you should definitely never use a local variable from a function in your main code. If you want to access error you should return it as such:

def errorcheck(player_input):
    if player_input in actions:
        error = False
    else:
        error = True
        while error == True:
            print('i dont know what you mean by',player_input)

            player_input = input('>')

            if player_input in actions:
                error = False
            else:
                error = True
    return error

Secondly it is no wonder that your program stops after entering help since after that there is no more code. If you want the user to be continuously be required to enter something you must put a loop around the whole parsing logic...

mrCarnivore
  • 4,638
  • 2
  • 12
  • 29
  • "you should definitely never use a local variable from a function in your main code" I'm not sure what this means. You *can't* use a local variable outside of its scope, right? Does this mean the code will crash? – en_Knight Jan 25 '19 at 15:49
  • This will still have the wrong behavior since the main code depends on the modified value of `player_input ` – DeepSpace Jan 25 '19 at 15:50
  • @en_Knight: You are right. I phrased it too softly! My code fixes that problem, tough. – mrCarnivore Jan 25 '19 at 15:51
  • @DeepSpace: Yes my code fixes only the most obvious problems since a complete fix would require a complete re-write of the code. Also the OP uses very strange constructs like the while-loop at the end... – mrCarnivore Jan 25 '19 at 15:52