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)
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
ifstatement. 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 aif/elif/elseso that once it hits one, it skips the others, or you need to have acontinueafter any successful check, since if you find a match, you just want to continue with the loop.So something like: