我试图在Python中进行简单的“点移动”,在特定的符号上使用自动增量,但它不能正常工作(位置保持不变),所以我需要一点帮助
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我想得到位置的变化与列表中的每个符号,所以如果输入将是"U,U,U“y新的位置将是0,3
发布于 2019-02-01 17:19:41
试试这个。
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;https://stackoverflow.com/questions/54474185
复制相似问题