Hello,

I found this code in a thread from about 18 months ago in a post by 'woooee' -

def func_1():
   print "     func_1 test"
def func_2():
   print "     func_2 test"
def func_3():
   print "     func_3 test"
def func_4():
   print "     func_4 test"

##----------------------------------------------------------------
##   dictionary = list of function to call and menu choice to print
menu_d = { 1:['func_1()', '1)Load'], 
           2:['func_2()', '2)Save'], 
           3:['func_3()', '3)Add Phone Number'],
           4:['func_4()', '4)View Phone Number'],
           5:["", "5)Exit"]}
choice = 0
while choice != 5:
   print "\nSelect an Option\n"
   for j in range(1, 6):
      print menu_d[j][1]   ## print list's element #1 for key=j

   choice = int(raw_input( "Enter choice, 1-5 " ))
   if choice in menu_d:
      dict_list = menu_d[choice]
      eval(dict_list[0])
      ## eval(menu_d[choice][0])   will also work
print "\nend of program"

Options 1-4 work fine. However, when I choose option 5 I get this -

Traceback (most recent call last);
   File "/home/......... .py", line 101, in <module>
     eval(dict_list[0])
   File "<string>", line 0

    ^
SyntaxError: unexpected EOF while parsing

I've checked and double checked for typo(s). I'm afraid I've not understood the answers I've found from Google!

Any pointers much appreciated.

Tim

Changing the 5th dictionary element to:

5:["False", "5)Exit"]

or

5:["None", "5)Exit"]

seems to the the trick.

From what I quickly read about the eval() function is that it runs the expression you give it in string format, or something similar to this. Since a blank line was giving it trouble I tried making it do nothing by feeding it False or None. Both seem to work, but I'm sure someone else can give a better explanation on how to fix it.

Edited 5 Years Ago by inputbear: n/a

Thank you for that.

I'll try it when I next get to the machine (about 11 hours from this post)

Tim

Another way: Break from the loop immediately when choice == '5' Another way: eval("sys.exit(0)") for choice 5
I would prefer, though: add

def func_5():
    print 'quitting'

and do the obvious thing on line 16

Edited 5 Years Ago by griswolf: n/a

As Gris said. I will do th same. Sometimes the problem as very coders faces is not the code but logic and with a litle thinking can help solve our problems.

You designed a very nice menu but complicated a simple exit. I just wonder how you couldnt see this little thing. Anyway thats my view.
happy coding :)

richieking - you've encapsulated my problem. I'm not a natural programmer. I'm an old fart who is trying to keep ones brain active. Unfortunately, my brain doesn't work the right (programmers) way!

In addition, I would like to be able to take credit for the menu but it wasn't mine. Someone by the handle 'woooee' posted it earlier and I tried to use it for my own 'program' (is it a program? What should it be called?)

However, thanks for reply.

Tim

I would prefer to have functions themselves as first value and just call it instead of using eval.

be sure to rename the functions according to choice. Best naming would be to use verb and if need to join words in name use _. Do not use capital letters.

Edited 5 Years Ago by pyTony: n/a

when the user hits enter it will display Price must be a valid positive number

product_price=input("Please enter product price:")
while product_price <=0 or product_price=="":
print"Price must be a valid positive number"
product_price=input("Enter product price:")

but it get the following error when the user hits enter
SyntaxError: unexpected EOF while parsing

This question has already been answered. Start a new discussion instead.