简体   繁体   中英

Python - index error in list

I am Writing Program that takes an input 2 thins 1st direction & 2nd is steps in one line, i am doing this by using split(' ') all these input takes in while loop but user dont want to entry more input he just enter the blank line and terminate but it is not happening dont know why... this is my code

while True:
movement = input().split(' ')
direction = movement[0].lower()
step = int(movement[1])
if movement != '' or movement != 0:

    if direction == 'up' or direction == 'down':
        if y == 0:
            if direction == 'down':
                y -= step
            else:
                y += step
        else:
            if direction == 'down':
                y -= step
            else:
                y += step
    elif direction == 'left' or direction == 'right':
        if x == 0:
            if direction == 'right':
                x -= step
            else:
                x += step
        else:
            if direction == 'right':
                x -= step
            else:
                x += step
else:
    current = (x, y)
    print(original)
    print(current)
    break

but i enter bank input it shows this message

 Traceback (most recent call last):
 File "C:/Users/Zohaib/PycharmProjects/Python Assignments/Question_14.py", 
 line 04, in <module>
 step = int(movement[1])
 IndexError: list index out of range

You can do a len() of your list and if you don't get any movement you make any logic, for example

if len(movement) == 0:
    # Your logic when you don't have any input
    pass
else:
    # Your logic when you have at least one input
    pass

Move your direction = movement[0].lower() lines into the if statement, this will allow them to run only if movement != '', you need to change your if statement o and instead otherwise it is always true as movement can't be '' and 0 at the same time

Also, move the split into the if statement as well, so that when comparing in the if statement, just comparing movement. (' '.split() returns [])

while True:

    movement = input()
    if movement != '' and movement != '0':
        movement = movement.split()
        direction = movement[0].lower()
        step = int(movement[1])
        del movement[1]
        if direction == 'up' or direction == 'down':
            if y == 0:
                if direction == 'down':
                    y -= step
                else:
                    y += step
            else:
                if direction == 'down':
                    y -= step
                else:
                    y += step
        elif direction == 'left' or direction == 'right':
            if x == 0:
                if direction == 'right':
                    x -= step
                else:
                    x += step
            else:
                if direction == 'right':
                    x -= step
                else:
                    x += step
    else:
        current = (x, y)
        print(original)
        print(current)
        break

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