Newby Python help needed with functions

Cathy James nambo4jb at gmail.com
Fri Jun 3 10:42:19 EDT 2011


I need a jolt here with my python excercise, please somebody!! How can I
make my functions work correctly? I tried below but I get the following
error:

if f_dict[capitalize]:

KeyError: <function capitalize at 0x00AE12B8>

Code below:



def capitalize (s):
    """capitalize accepts a string parameter and applies the capitalize()
method"""
    s.capitalize()
def title(s):
    """accepts a string parameter and applies the title() method"""
    s.title()
def upper(s):
    """accepts a string parameter and applies the upper() method"""
    s.upper()
def lower(s):
    """accepts a string parameter and applies the lower() method"""
    s.lower()
def exit():
    """ends the program"""
    import sys
    sys.exit()
if __name__ == "__main__":
    f_dict = {'capitalize': 'capitalize(s)',
          'title': 'title(s)',
          'upper': 'upper(s)',
          'lower': 'lower(s)',
          'exit': 'exit(s)'}
    options = f_dict.keys()
    prompt = 'Enter a function name from the list (%s): ' % ',
'.join(options)
while True:
    inp = input(prompt)
    option =f_dict.get(inp, None)#either finds the function in question or
returns a None object
    s = input ("Enter a string: ").strip()
    if not (option):
        print ("Please enter a valid selection")
    else:
        if f_dict[capitalize]:
            capitalize(s)
        elif f_dict [title]:
            title(s)
        elif f_dict[upper]:
            upper(s)
        elif f_dict [lower]:
            lower(s)
        elif f_dict[exit]:
            print ("Goodbye!! ")
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110603/7e61ecea/attachment.html>


More information about the Python-list mailing list