我有这个函数来检查我们在通过坐标系旅行时面对的方式,通过查看元组中的值如何增加或减少与路点列表中的下一个值相比较。看代码,它感觉复杂和笨拙:我如何缩短这个if语句?
a.facing = self.direction(a.travel_list[0], a.travel_list[1])
def direction(self, start, end):
s_width = start[0]
s_height = start[1]
e_width = end[0]
e_height = end[1]
# check directions
if s_height < e_height:
if s_width < e_width:
return 'right'
elif s_width > e_width:
return 'up'
else:
return 'up_right'
elif s_height > e_height:
if s_width < e_width:
return 'down'
elif s_width > e_width:
return 'left'
else:
return 'down_left'
elif s_height == e_height and s_width < e_width:
return 'down_right'
else:
return 'up_left'
返回值被调整为顺时针旋转一步。我的问题是,我们如何更改代码以使功能更短,更高效?
编辑:请注意,只能在指定的8个方向上进行移动。
“开始”和“结束”能够平等吗?你似乎没有正确处理这种情况。另外,我认为'elif s_height == e_height和s_width
jimhark
@jimhark我只在移动时调用这个函数,所以不需要改变面向方向。 –
我刚刚注意到(通过测试)你制作的字符串,这个,非直观的。 @ AshRj的生成结果对我来说更有意义,但也许我不完全理解您的要求(或者可能是定义)。那么你能解释为什么'如果s_height
jimhark