user1234089213048
user1234089213048

Reputation: 11

My if-elif-else statement in Python is not working properly

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.

Upvotes: 1

Views: 6342

Answers (3)

1cedsoda
1cedsoda

Reputation: 642

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")

Upvotes: 0

user812786
user812786

Reputation: 4430

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')

Upvotes: 3

Kasravnd
Kasravnd

Reputation: 107287

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.

Upvotes: 2

Related Questions