[Tutor] changing list index start

Joel Goldstick joel.goldstick at gmail.com
Sat Sep 11 17:40:38 CEST 2010


On Sat, Sep 11, 2010 at 11:15 AM, Steven D'Aprano <steve at pearwood.info>wrote:

> On Sat, 11 Sep 2010 11:25:12 pm Rance Hall wrote:
>
> > Thanks everyone for responding,  Because this menu structure is
> > repeated many times in my code, the ideal solution would have been to
> > "set index start = 1" in the beginning of the script.
>
> That is exactly the wrong solution. That will break anything and
> everything that assumes "set index start = 0" is applying. Fortunately
> Python doesn't allow such a bad "solution".
>
> The right solution is to get rid of all that duplicated code. Put the
> menu structure in one place, a function, and then call that function
> whenever you need it:
>
>
> def display_menu(menu):
>    for i,option in enumerate(menu, 1):
>         print('%s. %s' % (i, option))
>     choice = int(input('\nYour Choice? '))
>    clearscreen(osname)
>     return choice-1
>
>
> Now your mainmenu function becomes:
>
> def mainmenu():
>     # the main menu, in case you can't read the function name
>     todolist()  # why is this here?
>     menu = ['Clients','Jobs','Billing','Quotes','To Do
> Items','Employee','Exit']
>     calls = [clientsmenu, jobsmenu, billingmenu, quotesmenu,
> todomenu, empmenu, quit]
>     n = display_menu(menu)
>     calls[n]()
>
>
> And similarly for your other menus:
>
> def secondmenu():
>    menu = ['About','Help','Exit']
>    calls = [aboutmenu, helpmenu, quit]
>    n = display_menu(menu)
>    calls[n]()
>
>
>
>
> > something like sysctl variables in Linux perhaps but in this case
> > only valid for this program.
> >
> > Its clear from the responses that this solution is not available in
> > python, I wish it were, it would make my life much easier for this
> > project.
>
> No, you only *think* it would make your life easier. This is probably
> the time to quote Yoda's speech about the Dark Side of the Force
> from "The Empire Strikes Back".
>
> Such global settings are "easier, faster, simpler"... for about fifteen
> minutes. The right solution is to build reusable building blocks, then
> put them together.
>
>
> [...]
> > Lie also referred to my particular case as a valid exception, are
> > there enough other such valid exceptions that requesting a feature
> > enhancement would gain some traction?
>
> Not a hope in hell.
>
> You have misunderstood Lie's comment. He's talking about the use of an
> index *at all*. Normally in Python you shouldn't need to use indexes,
> regardless of whether they start with 0 or 1 or 3.1425.... Your example
> of a text menu is an exception to the rule (more of a guideline
> really) "you shouldn't care about indexes".
>
>
>
> --
> Steven D'Aprano
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>


I think the first message in the original post is instructive:

"I'm using the following function style I found on the net to create
menus for a command line python script:"

I (sometimes!) love looking at other peoples code to learn.  However taking
some code and
using it in your application demands that you understand it, or it will bite
you later.  In this case,
the code might have been appropriate to the original author, but not for its
new use.

Using index starting with 1 sounds to me idiomatic of BASIC programming.
While I'm too inexperienced
with Python to consider myself good at it, I think the key to using python
is to 'get it' as to the fundamental
data types in python and how they make things easier to solve problems.
One of the earliest programming
books I read was 'Algorithms + Data Structures = Programs' by Niklaus
Wirth.  The book used Pascal, which I think
the author wrote.  But no matter the language, learn what data structures it
offers, and why and then your algorithms
will become simpler and more elegant.  If you find yourself doing weird
things with your code, see if you can rethink
how you organize your data



-- 
Joel Goldstick
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100911/a4a2daf2/attachment.html>


More information about the Tutor mailing list