简体   繁体   中英

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.

Two issues here. The first, as noted by the other answers, is that you're comparing an int to a string. So, wrap your input s 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')

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM