[Tutor] working with empty lists

Alan Gauld alan.gauld at btinternet.com
Thu Sep 16 09:47:49 CEST 2010


"Rance Hall" <ranceh at gmail.com> wrote

> Here is what I've got so far:
>
> l = []
> for i in range(0,10)
>    l.append(i)
>
> for i in range(0,10)
>    print('%s. %s' % (i, l[i])
>
> I tried changing the append to l.append(i+1)
>
> which almost worked but the output started with 1. 2  I was curious
> what happend to the 0. 1 line

You made a mistake somewhere, it works as you expected
if you only make that one change.

> I know generally that we aren't supposed to care about the indexes 
> but
> this is eventually going to be part of a menuing system that 
> displays
> the index, so I do have a legit need to care about what is happening
> to the list index.

That's probably a bad idea. It's confusing for users if the number of 
their
favourite command changes, as it would if you added a new command
into the list etc. (One of the big advantages of menu driven CLIs is 
that
users learn the sequences they use regularly and start to "type ahead"
which makes for a very efficient operation.) It's usually better to 
keep
the command number as part of the menu data and then sort the
command in order based on that.

Just a thought,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list