题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
import sys
s = input().split(';') #将输入的按照“;”分开单独计算
a = [0, 0] #定义初始坐标
for i in s: #遍历各命令(其中分为空,无效,有效指令)
if not i: #分类讨论,空命令坐标不变(空无法按照字符串操作)
a = a
elif i[0] in ('A', 'D', 'W', 'S') and (i[1:]).isdigit():
#分类有效命令字符串格式:“A/D/W/S”+数字(判断i[1:]均为数字)
if i[0] == 'A':
a[0] = a[0] - int(i[1:])
elif i[0] == 'D':
a[0] = a[0] + int(i[1:])
elif i[0] == 'W':
a[1] = a[1] + int(i[1:])
elif i[0] == 'S':
a[1] = a[1] - int(i[1:])
else: #其他均为无效命令字符串
a = a
print(str(a[0]) + ',' + str(a[1]))
#输出格式需要“,”隔开。即坐标需要字符串格式“str()”