题解 | #坐标移动#

坐标移动

https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29

为什么提示坐标没有移动?

--- 因为在step = item[1]的时候 是str 需要 int()

step = int(item[1:])

input_list = input().split(";")
initial = [0,0]

for item in input_list:
    if not 2<=len(item)<=3:
        continue
    try:
        direction = item[0]
        step = int(item[1:])
        if direction in ["A","D","S","W"]:
            if 0<= step <=99:
                if direction=='A':
                    initial[0] -=step
                elif direction=='D':
                    initial[0] +=step
                elif direction=='W':
                    initial[1]+=step
                elif direction=='S':
                    initial[1]-=step 

    except:
        continue  
print(str(initial[0])+','+str(initial[1]))

# input_list= input().split(';')
# initial=[0,0]

# for item in input_list:
#     if not 2<=len(item)<=3:
#         continue
#     try:
#         direction=item[0]
#         step =int(item[1:])
#         if direction in ['A','D','W','S']:
#             if 0<= step <=99:
#                 if direction=='A':
#                     initial[0] -=step
#                 elif direction=='D':
#                     initial[0] +=step
#                 elif direction=='W':
#                     initial[1]+=step
#                 elif direction=='S':
#                     initial[1]-=step 
#     except:
#         continue 
# print(str(initial[0])+','+str(initial[1]))

全部评论

相关推荐

点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务