题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
a=input().split(';') b1=[] c=['A','D','W','S'] for i in a: if len(i)>=1: v1 = i[0] v2 = i[1:] else:continue if v1 in c and v2.isdigit() and int(v2) < 100: if v1=='A': value = [-1, 0] elif v1=='D': value = [1, 0] elif v1 =='W': value = [0,1] elif v1 =='S': value = [0, -1] else : value = [0,0] value[0]=int(value[0])*int(v2) value[1]=int(value[1])*int(v2) b1.append(value) aa,bb =0 ,0 for j in b1: aa=aa+j[0] bb=bb+j[1] print('%d,%d'%(aa,bb))
1.c=['A','D','W','S'] 定义方向的字符必须带引号,否则会被当做变量处理且变量还没定义。同时输入与匹配也需要对应
2. 在第5行中 if len(i)>=1:防止输入的字符串拆分后数组越界导致列表为空或者只有一个元素的时候报异常