Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ben Finney Newsgroups: comp.lang.python Subject: Re: Python Madlibs.py code and error message Date: Thu, 28 Apr 2016 15:32:51 +1000 Lines: 33 Message-ID: References: <3cff626c-28a2-499c-9877-de2df4d459d0@googlegroups.com> <6c0d96c5-c7f5-4659-a2e9-20f8d202d701@googlegroups.com> <1461820617.3238425.591942249.1484DAC3@webmail.messagingengine.com> <85fuu6jo1o.fsf@benfinney.id.au> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: news.uni-berlin.de 3dVOFA3RDwKMbclcOmg3ewv6fdNPFusqR2R5SZGpPwHg== Cancel-Lock: sha1:wcqiw0OaMvONzyrOVYK9r55cmCQ= Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'string.': 0.04; 'subject:Python': 0.05; 'formatting': 0.07; 'subject:code': 0.07; '"my': 0.09; 'positional': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:error': 0.11; 'argument': 0.15; '"bring': 0.16; '"first,': 0.16; '(ie,': 0.16; 'arguments;': 0.16; 'better:': 0.16; 'examples:': 0.16; 'feasible': 0.16; 'formatting.': 0.16; 'master.': 0.16; 'mismatch': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'shalt': 0.16; 'url.': 0.16; '{0}': 0.16; '{0}"': 0.16; '{name}"': 0.16; '{}"': 0.16; 'string': 0.17; '(not': 0.20; 'arguments': 0.22; 'stephen': 0.22; 'code,': 0.23; 'leave': 0.23; 'proprietary': 0.23; 'references': 0.23; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'error': 0.27; 'parameters': 0.27; 'implicitly': 0.29; "i'm": 0.30; 'url:python': 0.33; 'url:org': 0.36; 'keyword': 0.36; 'url:library': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'being': 0.37; 'received:org': 0.37; 'missing': 0.37; 'means': 0.39; 'format': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'software': 0.40; 'url:3': 0.60; 'your': 0.60; 'making': 0.62; 'more': 0.63; 'between': 0.65; 'packages,': 0.66; 'choose': 0.68; 'skip:\xe2 10': 0.70; '_o__)': 0.84; 'received:125': 0.84; 'url:string': 0.84 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: jigong.madmonks.org X-Public-Key-ID: 0xAC128405 X-Public-Key-Fingerprint: 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405 X-Public-Key-URL: http://www.benfinney.id.au/contact/bfinney-pubkey.asc X-Post-From: Ben Finney User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: <85fuu6jo1o.fsf@benfinney.id.au> X-Mailman-Original-References: <3cff626c-28a2-499c-9877-de2df4d459d0@googlegroups.com> <6c0d96c5-c7f5-4659-a2e9-20f8d202d701@googlegroups.com> <1461820617.3238425.591942249.1484DAC3@webmail.messagingengine.com> Xref: csiph.com comp.lang.python:107762 Stephen Hansen writes: > The error message means there's a mismatch between the number of > formatting instructions (ie, %s) and arguments passed to formatting. I > leave it to you to count and find what's missing or extra, because I'm > seriously not going to do that :) Better: when you have many semantically-different values, use named (not positional) parameters in the format string. Some simple format string examples: "First, thou shalt count to {0}" # References first positional argument "Bring me a {}" # Implicitly references the first positional argument "From {} to {}" # Same as "From {0} to {1}" "My quest is {name}" # References keyword argument 'name' […] By using names, you will not need to count positional arguments; and when there's an error, the error will state the name, making it easier to debug. Also feasible with ‘%’ syntax. But, if you're writing new code, you may as well use the more powerful ‘str.format’ described at that URL. -- \ “To have the choice between proprietary software packages, is | `\ being able to choose your master. Freedom means not having a | _o__) master.” —Richard M. Stallman, 2007-05-16 | Ben Finney