Q:

Calling a function from dictionary via user input - ValueError raised more often than I want

I wrote the following code to execute a function from a dictionary by user input. There is a serial connection involved, that's why a close serial port command is involved later, don't be bothered by this.
from commands import test1, test2, test3, finangle, Testsequence
    dict = {
       'test1':test1,
       'test2':test2, 
       'test3':test3,
       'finangle':finangle,
       'Testsequence':Testsequence
    }

The dictionary is used to limit the things the user can do with his input.
def select_function(): 
    while True:
        try:
            cmd = input("Now please type in function name. \nType in 'end' to end the program. ")
            if cmd == 'end':
                ser.close() # close serial connection
                print("Connection to serial port " + ser.portstr + " closed.")
                sys.exit()
            if cmd in dict:
                return dict[cmd]()
        except:
            raise ValueError('Invalid input!')

select_function()

As far as I know this whole thing has to be a function because I want to use 'return' and this command can't be called outside of a function.
So I tried to give the user a choice to either call one of the 5 functions or 'end', which closes the serial connection and aborts the rest of my script (sys.exit). If the user input isn't 'end' or in the dictionary a ValueError and 'Invalid input!' will be raised!
However, ValueError is raised if I type in 'end' and well as 'Testsequence' or 'finangle'. 'test1', 'test2', 'test3' are working fine. I am 100% sure that 'Testsequence' and 'finangle are defined correctly, I checked it several times. Also I am pretty sure that I can write 'end' correctly. Can anybody see what I am doing wrong?
Also I want to give the user a second choice after the input was invalid. And he has to be informed about this. However, if I write it like
except:
    raise ValueError('Invalid input! Please try again!')
    select_function()

the user gets another chance, but the error message is not raised! How can I fix this?
EDIT:
Problem solved. I feel bad, because it was just a misunderstanding of python basics. Thanks Daniel Roseman. Here is the resulting code:
def select_function(): # has to be a function because of 'return'
    while True:
        cmd = input("Now please type in function name. \nType in 'end' to end the program. ")
        if cmd == 'end':
                print("Connection to serial port closed.")
                sys.exit()
        if cmd in dict:
                return dict[cmd]()
        else:
            print('Invalid input! Please try again!')
            select_function()

select_function()

A:

Almost certainly, your except is catching other problems that you're not expecting - probably, a NameError because ser is not defined.
But there is no point doing what you are doing; dict[cmd] would already raise ValueError; it is pointless catching things only to raise that exception. Remove that try/except completely.

