Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #6952

Re: Newby Python help needed with functions

Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python.list@tim.thechases.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'subject:Python': 0.06; 'dictionary': 0.07; 'python': 0.08; 'none)': 0.09; 'none:': 0.09; 'def': 0.12; 'error:': 0.12; 'am,': 0.14; 'wrote:': 0.14; 'defined': 0.14; '(sorry': 0.16; '-tkc': 0.16; 'error)': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'keyerror:': 0.16; 'message-id:@tim.thechases.com': 0.16; 'cc:addr :python-list': 0.17; 'tree': 0.19; '(which': 0.20; 'subject:help': 0.20; 'header:In-Reply-To:1': 0.21; 'variable': 0.21; 'cc:2**0': 0.22; 'cc:no real name:2**0': 0.23; 'item.': 0.23; 'keys': 0.23; 'index': 0.25; 'function': 0.25; 'string': 0.26; 'tried': 0.27; '(not': 0.28; 'changing': 0.28; 'cc:addr:python.org': 0.30; 'define': 0.31; "skip:' 10": 0.32; '...': 0.34; 'option': 0.35; 'header:User-Agent:1': 0.35; 'function.': 0.35; 'skip:{ 10': 0.35; 'too.': 0.37; 'something': 0.37; 'change': 0.37; 'but': 0.38; 'subject:: ': 0.38; 'received:209': 0.39; 'map': 0.39; 'subject:with': 0.39; 'allows': 0.40; 'your': 0.60; 'below': 0.61; 'here': 0.66; '(s):': 0.84; '09:42': 0.84; 'correctly?': 0.84; 'dict,': 0.84
Date Fri, 03 Jun 2011 10:18:43 -0500
From Tim Chase <python.list@tim.thechases.com>
User-Agent Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.17) Gecko/20110414 Thunderbird/3.1.10
MIME-Version 1.0
To Cathy James <nambo4jb@gmail.com>
Subject Re: Newby Python help needed with functions
References <BANLkTinpQGQ0wtdkLuVegy_SmyvurQs-yg@mail.gmail.com>
In-Reply-To <BANLkTinpQGQ0wtdkLuVegy_SmyvurQs-yg@mail.gmail.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-AntiAbuse This header was added to track abuse, please include it with any abuse report
X-AntiAbuse Primary Hostname - boston.accountservergroup.com
X-AntiAbuse Original Domain - python.org
X-AntiAbuse Originator/Caller UID/GID - [47 12] / [47 12]
X-AntiAbuse Sender Address Domain - tim.thechases.com
X-Source
X-Source-Args
X-Source-Dir
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2433.1307114339.9059.python-list@python.org> (permalink)
Lines 47
NNTP-Posting-Host 82.94.164.166
X-Trace 1307114339 news.xs4all.nl 49178 [::ffff:82.94.164.166]:43108
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:6952

Show key headers only | View raw


On 06/03/2011 09:42 AM, Cathy James wrote:
> 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>
>
> def capitalize (s):

Here you define the variable "capitalize" as a function.

>      f_dict = {'capitalize': 'capitalize(s)',

Here your dictionary's key is a *string* "capitalize" (not the 
variable defined above)

>          if f_dict[capitalize]:

Here you use the function variable as an index into this dict, 
not the string (which is the key that the dictionary contains).

Changing this to

   if f_dict["capitalize"]:

it will find the item.  Note that you'd have to change the other 
keys too.

Finally, note that Python allows you to do function dispatch, 
collapsing your entire if/elif tree to something like

   f_dict = {# map of strings to the function-variable name
     'capitalize': capitalize,
     ...
     }
   option = f_dict.get(inp, None)
   if option is None:
     do_whatever_error_processing(inp)
   else: # "option" is the function so we can call directly
     option(s)

-tkc

(sorry if a dupe...got an SMTP error)

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Newby Python help needed with functions Tim Chase <python.list@tim.thechases.com> - 2011-06-03 10:18 -0500

csiph-web