how do I get the mov function to work properly

66 views Asked by At

So, I have this input command that's supposed to give the user unprompted input and if it's an invalid command to output 'input' is an invalid command. However, go north, west, etc. is a valid command but the code does not recognize it as so. Pls help :)

btw, player is a module that has a class in it for moving the player's location.

Code:

import world, tiles
from player import Player

game = "play"
while game == "play":
    x = input()
    y = " is not a valid command"
    string = x + y
    
    if x == "go north":
        Player.go_north

    if x == "go south":
        Player.go_south
    if x == "go east":
        Player.go_east

    if x == "go west":
        Player.go_west

    if x == "pick up":
        print("pick up what?")

    else:
        print(string)
1

There are 1 answers

3
saquintes On

There seems to be a couple of things wrong potentially. First, Player.go_* are being referenced as attributes, but I expect they should be functions if they do something? Technically I guess they could be properties, but this feels like the wrong way to use them.

Second, I think your if/else logic is not what you want. You check for each direction, but even if it suceeds, it drops down to the next if statement. So what happens is at the last check, if it's not "pick up", it will always print the invalid command string. You either want every conditional to be part of a if/elif/else so that once it hits one, it skips the others, or you need to have a continue after any successful check, since if you find a match, you just want to continue with the loop.

So something like:

import world, tiles
from player import Player

player = Player()
game = "play"
while game == "play":
    x = input()
    y = " is not a valid command"
    string = x + y
    
    if x == "go north":
        player.go_north()
    elif x == "go south":
        player.go_south()
    elif x == "go east":
        player.go_east()
    elif x == "go west":
        player.go_west()
    elif x == "pick up":
        print("pick up what?")
    else:
        print(string)