I’ll post my code first, and then ask questions.
def menu(**arg):
if len(arg) == 0:
name = raw_input("Enter your name: ")
location = raw_input("Enter your name: ")
else:
for i,j in arg.items():
globals()[i] = j
print "Name: %s | Location: %s" % (name, location)
The goal of this function is to print out something like this:
Name: someone | Location: somewhere
If I just input
menu()
OK, no problem, but if I type
menu(name=’someone’, location=’somewhere’)
something went wrong…
If I rewrite it like this:
def menu(**arg):
if len(arg) == 0:
pass
else:
for i,j in arg.items():
globals()[i] = j
print "Name: %s | Location: %s" % (name, location)
and I type
menu(name=’someone’, location=’somewhere’)
it worked… but I don’t know why
Also, why can’t I replace vars() with globals() in the “rewritten version”?
And my last question is …
I found this code verbose and redundant..
Is there any way to make it cleaner and neater?
Thanks for your patience!
I’ll answer your last question:
For example: