L7Q2

 avatar
unknown
plain_text
a month ago
996 B
14
Indexable
def run():
    f = open(input("Choose your movefile: "), "r")
    x, y = input("Initial position : ").split(",")
    x = int(x)
    y = int(y)
    data = f.readlines()
    stop = False
    invalid = False

    for c in data:
        c = c.strip()
        if c == "L":
            x = x - 1
            if x == -10:
                stop = True
                x = x + 1
        elif c == "R":
            x = x + 1
            if x == 10:
                stop = True
                x = x - 1
        elif c == "D":
            y = y - 1
            if y == -10:
                stop = True
                y = y + 1
        elif c == "U":
            y = y + 1
            if y == 10:
                stop = True
                y = y - 1
        else:
            invalid = True
            break

    if invalid:
        print("Invalid command")
    elif stop:
        print(f"Robot stops at {x}, {y}")
    else:
        print(f"Robot stops at {x}, {y}")
Editor is loading...
Leave a Comment