[Tutor] Concatenating Strings into Variable Names?
Rich Krauter
rmkrauter at yahoo.com
Mon Feb 2 14:06:34 EST 2004
>
> print the_answer
>
You can test these out:
try:
print eval('the_answer')
except:
"Invalid choice"
or
print globals()['the_answer']
or
print locals()['the_answer']
or
print globals().get('the_answer',"Not a valid choice")
or
print locals().get('the_answer',"Not a valid choice")
I hope others comment on the right way - I'm interested in which is
considered the best, if any. The dictionary method posted by Don is good
too. The first and last two examples above provide a check for invalid
entries, similar to Don's method.
Good luck.
Rich
More information about the Tutor
mailing list