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


Groups > comp.lang.python > #107762

Re: Python Madlibs.py code and error message

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Ben Finney <ben+python@benfinney.id.au>
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 <mailman.175.1461821587.32212.python-list@python.org> (permalink)
References <3cff626c-28a2-499c-9877-de2df4d459d0@googlegroups.com> <dodj48Fb8o8U1@mid.individual.net> <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 <python-python-list@m.gmane.org>
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 <bignose+hates-spam@benfinney.id.au>
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 <python-list.python.org>
List-Unsubscribe <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID <85fuu6jo1o.fsf@benfinney.id.au>
X-Mailman-Original-References <3cff626c-28a2-499c-9877-de2df4d459d0@googlegroups.com> <dodj48Fb8o8U1@mid.individual.net> <6c0d96c5-c7f5-4659-a2e9-20f8d202d701@googlegroups.com> <1461820617.3238425.591942249.1484DAC3@webmail.messagingengine.com>
Xref csiph.com comp.lang.python:107762

Show key headers only | View raw


Stephen Hansen <me+python@ixokai.io> 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'
    […]

    <URL:https://docs.python.org/3/library/string.html#formatstrings>

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

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


Thread

Python Madlibs.py code and error message Cai Gengyang <gengyangcai@gmail.com> - 2016-04-27 21:37 -0700
  Re: Python Madlibs.py code and error message Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2016-04-28 16:50 +1200
    Re: Python Madlibs.py code and error message Cai Gengyang <gengyangcai@gmail.com> - 2016-04-27 22:01 -0700
      Re: Python Madlibs.py code and error message Stephen Hansen <me+python@ixokai.io> - 2016-04-27 22:16 -0700
      Re: Python Madlibs.py code and error message Ben Finney <ben+python@benfinney.id.au> - 2016-04-28 15:32 +1000
      Re: Python Madlibs.py code and error message Stephen Hansen <me@ixokai.io> - 2016-04-27 23:50 -0700
      Re: Python Madlibs.py code and error message Ben Finney <ben+python@benfinney.id.au> - 2016-04-28 16:55 +1000
      Re: Python Madlibs.py code and error message Stephen Hansen <me@ixokai.io> - 2016-04-28 00:08 -0700
        Re: Python Madlibs.py code and error message Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-04-28 17:25 +1000
          Re: Python Madlibs.py code and error message Cai Gengyang <gengyangcai@gmail.com> - 2016-05-03 04:12 -0700
      Re: Python Madlibs.py code and error message Ben Finney <ben+python@benfinney.id.au> - 2016-04-28 17:17 +1000
  Re: Python Madlibs.py code and error message Stephen Hansen <me+python@ixokai.io> - 2016-04-27 21:55 -0700

csiph-web