Python Madlibs.py code and error message
Ben Finney
ben+python at benfinney.id.au
Thu Apr 28 01:32:51 EDT 2016
Stephen Hansen <me+python at 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
More information about the Python-list
mailing list