Search code examples
pythonpython-3.xgridauto-increment

Problem with changing point position in Python


i'm trying to make simple "point move" in Python, using autoincrement on specific symbol, but it's not working properly(position stays the same), so I need a little help here

import re

while True:
    m=input(str("How robot should move? (use U,D,L,R to move): "))
if not re.match("^[U,D,L,R]*$", m):
    print("WRONG MOVE! USE -> U,D,L,R")
if re.match("^[U,D,L,R]*$", m):
    moves = list(m.split())
    print(moves)

    x = 0
    y = 1
    position = [x, y]

    for U in moves:
        if U == "U":
            y+=y
        print(position)
        break

I want to get position change with every symbol in list, so if input will be "U,U,U" y new position will be [0,3]


Solution

  • try this...

    x = list(map(str,input().split(",")))
    
    a = 0
    b = 0
    
    for i in x:
        if (i=='U'):
            b+=1
            print(a,b)
        elif (i=='D'):
            b-=1
            print(a,b)
        elif (i=='L'):
            a-=1
            print(a,b)
        elif (i=='R'):
            a+=1
            print(a,b)
        else:
            print("wrong move")
            break;