我怎么能减少这段代码的长度? [英] How could I reduce the length of this code?
本文介绍了我怎么能减少这段代码的长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要尽可能地减少Python3中此代码的长度(即使它的可读性较差):
I need to reduce the length of this code in Python3 as much as possible (even if it will be less readable):
a,b,x,y=[int(i) for i in input().split()]
while 1:
r=''
if y<b:r='S';y+=1
if y>b:r='N';y-=1
if x<a:r+='E';x+=1
if x>a:r+='W';x-=1
print(r)
这是一张地图:你在(x,y),你需要去北(东北)的南北的(a,b)S东北....每次转弯后我必须告诉我去哪里使用打印。
例如我可以将所有if放在一行吗?
It's a map: you are on (x,y) and you need to go to (a,b) S for South N for North NE for North East.... After each turn I must tell where to go using print.
For example could I put all the if on one line ?
推荐答案
可以省去变量r
,因为没有必要。您还可以改进while循环,使其实际完成,如下所示:
The variabler
can be dispensed with as it is not necessary. You could also improve the while loop so it actually completes, as follows:
while (a != x or b != y):
if y>b:y-=1;print('North', y)
if y<b:y+=1;print('South', y)
if x<a:x+=1;print('East', x)
if x>a:x-=1;print('West', x)
这篇关于我怎么能减少这段代码的长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文