[Tutor] trivial simple program..can it be made more concise?

Mark Lawrence breamoreboy at yahoo.co.uk
Sat Feb 14 08:48:56 CET 2015


On 14/02/2015 03:17, steve10brink at comcast.net wrote:
> Hi all,
>
> I was playing with Python tonight and created a simple program that outputs numbers counting up then counting down all on the same terminal line. The code is as follows:
>
>
>
> #------------------------------------------------------------
> a = 320000 #number to count up to
>
>
> for i in range (a):
> print i, '\r',
>
> for i in range ((a-1),0,-1):
> print i, '\r',
>
> #------------------------------------------------------------
>
> It works as desired. However, I was trying to figure out a way to make it more concise but
> cannot see a way since the 'range' parameters must be integers (no functions allowed?).

If the functions return integers that's fine.

for i in range(int('0'), int('5'), int('1')):print(i)
0
1
2
3
4

>
> Anyone see a way to simplify it?
>

Well you could have:-

a = 5
for start, stop, step in ((0, a, 1), (a-1, 0, -1)):
     for i in range(start, stop, step):
         print(i)

0
1
2
3
4
4
3
2
1

But is it actually simpler?  I find your original code far easier to 
read so quite frankly I'd leave it as is.  If it ain't broke, don't fix 
it :)

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence



More information about the Tutor mailing list