题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
string = input().split(';') x,y = 0, 0 for n in string: # print(n, x, y) if n == '': continue elif not n[0] in ['A', 'S', 'W', 'D']: continue else: try: step = int(n[1:]) # print(step) if n[0] == 'A': x -= step elif n[0] == 'D': x += step elif n[0] == 'W': y += step else: y -= step except: continue print('{},{}'.format(x, y))
采用try-except判断第一个字母后是否是一个完整的数字