Question

So, here's the relevant code:

def action():
    print("These are the actions you have available:")
    print("1 - Change rooms")
    print("2 - Observe the room (OUT OF ORDER)")
    print("3 - Check the room(OUT OF ORDER)")
    print("4 - Quit the game")

    choice = input("What do you do?\n> ")
    if choice in (1, "move"):
        return(1)
    elif choice in (4, "quit"):
        return(4)

play = True

while play == True:
    ###some functions that aren't relevant to the problem
    choice = action()
    print(choice)
    if choice == 1:
        change_room()
        ## more code...

The function always returns None. I put the print(choice) in there to see what value "choice" had and it always prinss none, and the "if choice == 1" block never runs. So I guess the function isn't returning a value at all, so the mistake is probably at the return()'s in action(), but I've checked here and elsewhere and I can't see what's wrong with it.

Was it helpful?

Solution

input() returns a string, always, but you are testing against integers. Make them strings:

if choice in ('1', "move"):
    return 1

Your entered choice doesn't match any of your tests, so the function ends without ever reaching an explicit return statement and Python reverts to the default return value of None.

Better still, replace the whole if/elif/elif tree with a dictionary:

choices = {
    '1': 1,
    'move': 1,
    # ...
    '4': 4,
    'quit': 4,
}

if choice in choices:
    return choices[choice]
else:
    print('No such choice!')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top