1
votes

Here is my code:

def moveEntity(entity):
    print('forward=1')
    print('backward=2')
    print('left=3')
    print('right=4')
    direction = input('Where would you like to move?')
    distance = input('How many units?')

    if direction == 1:
        entity.y = entity.y + distance
    elif direction == 2:
        entity.y = entity.y - distance
    elif direction == 3:
        entity.x = entity.x - distance
    elif direction == 4:
        entity.x == entity.x + distance
    else:
        print('invalid input')

When I run this function, and input any of the 4 options (1,2,3,4), the function always skips the 4 if/elif statements and executes the else statement. I can't figure out what is wrong with the code that I posted above. I have tried printing the values of the variables "direction" and "distance" after they have been inputted, and they both printed as the correct values. Directly after that, despite running through the if and elif statements, the else statements was still executed. Any help would be appreciated.

3
Your update for case 4 uses ==, not =, so it will compare entity.x instead of assigning a new value to it.user812786

3 Answers

3
votes

Two issues here. The first, as noted by the other answers, is that you're comparing an int to a string. So, wrap your inputs with int. The second is that you had == in the last assignment, so even if it got to that case, it would not update the value of entity.x. This code should work:

def moveEntity(entity):
    print('forward=1')
    print('backward=2')
    print('left=3')
    print('right=4')
    direction = int(input('Where would you like to move?'))
    distance = int(input('How many units?'))

    if direction == 1:
        entity.y = entity.y + distance
    elif direction == 2:
        entity.y = entity.y - distance
    elif direction == 3:
        entity.x = entity.x - distance
    elif direction == 4:
        entity.x = entity.x + distance
    else:
        print('invalid input')
2
votes

It's because of that input returns a string, thus you need to convert the input to integer to compare with that numbers or just compare with string numbers, Also note that you need to convert the distance to integer before putting it in calculation :

def moveEntity(entity):
    print('forward=1')
    print('backward=2')
    print('left=3')
    print('right=4')
    direction = input('Where would you like to move?')
 while True:
  try :
    distance = int(input('How many units?'))
    if direction == '1':
        entity.y = entity.y + distance
    elif direction == '2':
        entity.y = entity.y - distance
    elif direction == '3':
        entity.x = entity.x - distance
    elif direction == '4':
        entity.x == entity.x + distance
    #return value
  except ValueError::
    print('please enter a valid digit')

Note that when you convert an input to an int it can raise an values error so for handling this issue you can use a try-except expression.

0
votes

That's because the input is a string, but the values in your if loop are integers.

Bad:

a = input()
if a == 1:
    print("hello world")

Good:

a = input()
if a == "1":
    print("hello world")