In the Absence of a GoTo Statement: Newbie needs menu-launcher to choose amongst sub-programs
Alex Martelli
aleaxit at yahoo.com
Fri Apr 13 18:05:36 EDT 2001
"Ron Stephens" <rdsteph at earthlink.net> wrote in message
news:3AD6161F.16F5AC19 at earthlink.net...
[snip]
> Thanks Alex!!!
You're welcone.
> I also am looking right now at your Python cookbook web site...wow this
> is a really great resource, thanks to you from me and from all
> Python learners and users future and present!!!
It IS great, I agree, but it's not mine -- it's ActiveState's, it's the
Python
community's, it's David Ascher's; I'm just one of many contributors, so,
direct these thanks to those who most deserve them!-)
> I guess in the case of the while loop above, I could nest the while loop
in a
> one bigger while loop that ask "do you want to run another program option
> quit?" Or, alternatively, and perhaps better, I could imbed a statement
> asking that question after the else code, immediately before the "break"
> statement, asking the same question. That would be better, I think...
I would let the user enter a 0 to indicate he or she wants to quit. Maybe
something like:
while 1:
print '0. quit'
for i in range(num_programs):
print '%d. %s' % (i, functions[i].__name__)
input_string = raw_input(prompt)
try:
input_code = int(input_string)
except:
print "Please enter a number"
else:
if 1<=input_code<=num_programs:
function = functions[input_code-1] + '.py'
function()
break
elif input_code == 0:
break
else:
print "Please, a number between 0 and", num_programs
where we also have a sub-loop reminding the user of available
choices, and a little error-diagnosis. These issues have little
to do with absence or presence of 'goto', but they do matter:-).
Alex
More information about the Python-list
mailing list