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)

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 ?

Recommended Answers

All 2 Replies

I don't think it can be reduced significantly. Why do you want a shorter code ?

Python is designed for readable code. If you want to write your entire code on one line, you have to pick another language. You can do it C.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.