Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #107755 > unrolled thread
| Started by | Cai Gengyang <gengyangcai@gmail.com> |
|---|---|
| First post | 2016-04-27 21:37 -0700 |
| Last post | 2016-04-27 21:55 -0700 |
| Articles | 12 — 6 participants |
Back to article view | Back to comp.lang.python
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
| From | Cai Gengyang <gengyangcai@gmail.com> |
|---|---|
| Date | 2016-04-27 21:37 -0700 |
| Subject | Python Madlibs.py code and error message |
| Message-ID | <3cff626c-28a2-499c-9877-de2df4d459d0@googlegroups.com> |
Python Madlibs.py code and error message --- Anyone can help? I keep getting stuck here ...
# This program does the following ... writes a Mad Libs story
# Author: Cai Gengyang
print "Mad Libs is starting!"
name = raw_input("Enter a name: ")
adjective1 = raw_input("Enter an adjective: ")
adjective2 = raw_input("Enter a second adjective: ")
adjective3 = raw_input("Enter a third adjective: ")
verb1 = raw_input("Enter a verb: ")
verb2 = raw_input("Enter a second verb: ")
verb3 = raw_input("Enter a third verb: ")
noun1 = raw_input("Enter a noun: ")
noun2 = raw_input("Enter a noun: ")
noun3 = raw_input("Enter a noun: ")
noun4 = raw_input("Enter a noun: ")
animal = raw_input("Enter an animal: ")
food = raw_input("Enter a food: ")
fruit = raw_input("Enter a fruit: ")
number = raw_input("Enter a number: ")
superhero_name = raw_input("Enter a superhero_name: ")
country = raw_input("Enter a country: ")
dessert = raw_input("Enter a dessert: ")
year = raw_input("Enter a year: ")
#The template for the story
STORY = "This morning I woke up and felt %s because _ was going to finally %s over the big _ %s. On the other side of the %s were many %ss protesting to keep %s in stores. The crowd began to _ to the rythym of the %s, which made all of the %ss very _. %s tried to _ into the sewers and found %s rats. Needing help, %s quickly called %s. %s appeared and saved %s by flying to %s and dropping _ into a puddle of %s. %s then fell asleep and woke up in the year _, in a world where %ss ruled the world."
print STORY % (Adjective1, name, Verb1, Adjective2, Noun1, Noun2, animal, food, Verb2, Noun3, fruit, Adjective3, name, Verb3, number, name , superhero_name, superhero_name, name, country, name, dessert, name, year, Noun4)
Terminal Output :
$ python Madlibs.py
Mad Libs is starting!
Enter a name: Cai Gengyang
Enter an adjective: beautiful
Enter a second adjective: honest
Enter a third adjective: huge
Enter a verb: hit
Enter a second verb: run
Enter a third verb: jump
Enter a noun: anger
Enter a noun: belief
Enter a noun: wealth
Enter a noun: regret
Enter an animal: elephant
Enter a food: burger
Enter a fruit: watermelon
Enter a number: 6
Enter a superhero_name: batman
Enter a country: America
Enter a dessert: icekachang
Enter a year: 1984
Traceback (most recent call last):
File "Madlibs.py", line 34, in <mo
dule>
print STORY % (Adjective1, name,
Verb1, Adjective2, Noun1, Noun2, an
imal, food, Verb2, Noun3, fruit, Adj
ective3, name, Verb3, number, name ,
superhero_name, superhero_name, nam
e, country, name, dessert, name, yea
r, Noun4)
NameError: name 'Adjective1' is not
defined
[toc] | [next] | [standalone]
| From | Gregory Ewing <greg.ewing@canterbury.ac.nz> |
|---|---|
| Date | 2016-04-28 16:50 +1200 |
| Message-ID | <dodj48Fb8o8U1@mid.individual.net> |
| In reply to | #107755 |
Cai Gengyang wrote:
> adjective1 = raw_input("Enter an adjective: ")
>
> NameError: name 'Adjective1' is not defined
Python is case-sensitive. You've spelled it "adjective1" in one
place and "Adjective1" in another. You need to be consistent.
--
Greg
[toc] | [prev] | [next] | [standalone]
| From | Cai Gengyang <gengyangcai@gmail.com> |
|---|---|
| Date | 2016-04-27 22:01 -0700 |
| Message-ID | <6c0d96c5-c7f5-4659-a2e9-20f8d202d701@googlegroups.com> |
| In reply to | #107756 |
I changed it to all lowercase, this time I get a different error message though (a TypeError message)
# This program does the following ... writes a Mad Libs story
# Author: Cai Gengyang
print "Mad Libs is starting!"
name = raw_input("Enter a name: ")
adjective1 = raw_input("Enter an adjective: ")
adjective2 = raw_input("Enter a second adjective: ")
adjective3 = raw_input("Enter a third adjective: ")
verb1 = raw_input("Enter a verb: ")
verb2 = raw_input("Enter a second verb: ")
verb3 = raw_input("Enter a third verb: ")
noun1 = raw_input("Enter a noun: ")
noun2 = raw_input("Enter a noun: ")
noun3 = raw_input("Enter a noun: ")
noun4 = raw_input("Enter a noun: ")
animal = raw_input("Enter an animal: ")
food = raw_input("Enter a food: ")
fruit = raw_input("Enter a fruit: ")
number = raw_input("Enter a number: ")
superhero_name = raw_input("Enter a superhero_name: ")
country = raw_input("Enter a country: ")
dessert = raw_input("Enter a dessert: ")
year = raw_input("Enter a year: ")
#The template for the story
STORY = "This morning I woke up and felt %s because _ was going to finally %s over the big _ %s. On the other side of the %s were many %ss protesting to keep %s in stores. The crowd began to _ to the rythym of the %s, which made all of the %ss very _. %s tried to _ into the sewers and found %s rats. Needing help, %s quickly called %s. %s appeared and saved %s by flying to %s and dropping _ into a puddle of %s. %s then fell asleep and woke up in the year _, in a world where %ss ruled the world."
print STORY % (adjective1, name, verb1, adjective2, noun1, noun2, animal, food, verb2, noun3, fruit, adjective3, name, verb3, number, name , superhero_name, superhero_name, name, country, name, dessert, name, year, noun4)
Terminal Output :
$ python Madlibs.py
Mad Libs is starting!
Enter a name: andrew
Enter an adjective: beautiful
Enter a second adjective: honest
Enter a third adjective: pretty
Enter a verb: hit
Enter a second verb: run
Enter a third verb: fire
Enter a noun: honesty
Enter a noun: love
Enter a noun: peace
Enter a noun: wisdom
Enter an animal: elephant
Enter a food: burger
Enter a fruit: watermelon
Enter a number: 1985
Enter a superhero_name: batman
Enter a country: america
Enter a dessert: icekachang
Enter a year: 1982
Traceback (most recent call last):
File "Madlibs.py", line 34, in <mo
dule>
print STORY % (adjective1, name,
verb1, adjective2, noun1, noun2, an
imal, food, verb2, noun3, fruit, adj
ective3, name, verb3, number, name ,
superhero_name, superhero_name, nam
e, country, name, dessert, name, yea
r, noun4)
TypeError: not all arguments converted during string formatting
On Thursday, April 28, 2016 at 12:50:28 PM UTC+8, Gregory Ewing wrote:
> Cai Gengyang wrote:
>
> > adjective1 = raw_input("Enter an adjective: ")
> >
> > NameError: name 'Adjective1' is not defined
>
> Python is case-sensitive. You've spelled it "adjective1" in one
> place and "Adjective1" in another. You need to be consistent.
>
> --
> Greg
[toc] | [prev] | [next] | [standalone]
| From | Stephen Hansen <me+python@ixokai.io> |
|---|---|
| Date | 2016-04-27 22:16 -0700 |
| Message-ID | <mailman.174.1461820620.32212.python-list@python.org> |
| In reply to | #107758 |
On Wed, Apr 27, 2016, at 10:01 PM, Cai Gengyang wrote: > I changed it to all lowercase, this time I get a different error message > though (a TypeError message) 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 :) -- Stephen Hansen m e @ i x o k a i . i o
[toc] | [prev] | [next] | [standalone]
| From | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| Date | 2016-04-28 15:32 +1000 |
| Message-ID | <mailman.175.1461821587.32212.python-list@python.org> |
| In reply to | #107758 |
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
[toc] | [prev] | [next] | [standalone]
| From | Stephen Hansen <me@ixokai.io> |
|---|---|
| Date | 2016-04-27 23:50 -0700 |
| Message-ID | <mailman.177.1461826249.32212.python-list@python.org> |
| In reply to | #107758 |
On Wed, Apr 27, 2016, at 10:32 PM, Ben Finney wrote:
> 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>
Except the poster is not using Python 3, so all of this is for naught.
--
Stephen Hansen
m e @ i x o k a i . i o
[toc] | [prev] | [next] | [standalone]
| From | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| Date | 2016-04-28 16:55 +1000 |
| Message-ID | <mailman.178.1461826551.32212.python-list@python.org> |
| In reply to | #107758 |
Stephen Hansen <me@ixokai.io> writes: > On Wed, Apr 27, 2016, at 10:32 PM, Ben Finney wrote: > > Better: when you have many semantically-different values, use named > > (not positional) parameters in the format string. […] > > > > <URL:https://docs.python.org/3/library/string.html#formatstrings> > > Except the poster is not using Python 3, so all of this is for naught. Everything I described above works fine in Python 2. Any still-supported version has ‘str.format’. -- \ “The deepest sin against the human mind is to believe things | `\ without evidence.” —Thomas Henry Huxley, _Evolution and | _o__) Ethics_, 1893 | Ben Finney
[toc] | [prev] | [next] | [standalone]
| From | Stephen Hansen <me@ixokai.io> |
|---|---|
| Date | 2016-04-28 00:08 -0700 |
| Message-ID | <mailman.179.1461827333.32212.python-list@python.org> |
| In reply to | #107758 |
On Wed, Apr 27, 2016, at 11:55 PM, Ben Finney wrote: > Stephen Hansen <me@ixokai.io> writes: > > > On Wed, Apr 27, 2016, at 10:32 PM, Ben Finney wrote: > > > Better: when you have many semantically-different values, use named > > > (not positional) parameters in the format string. […] > > > > > > <URL:https://docs.python.org/3/library/string.html#formatstrings> > > > > Except the poster is not using Python 3, so all of this is for naught. > > Everything I described above works fine in Python 2. Any still-supported > version has ‘str.format’. This response is completely unhelpful. The OP is using Python 2, and using %-formatting, and so you give a series of examples of using str.format, to, what? Confuse matters? You can show using non-positional values using the format the user is using -- "%(name)s" formatting. You even reference a link to the Python 3 docs, even though the OP is running code which isn't 3.x compatible. Confusion. Wishing Python2 away isn't helpful. -- Stephen Hansen m e @ i x o k a i . i o
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2016-04-28 17:25 +1000 |
| Message-ID | <5721baee$0$22141$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #107766 |
On Thursday 28 April 2016 17:08, Stephen Hansen wrote:
> On Wed, Apr 27, 2016, at 11:55 PM, Ben Finney wrote:
>> Stephen Hansen <me@ixokai.io> writes:
>>
>> > On Wed, Apr 27, 2016, at 10:32 PM, Ben Finney wrote:
>> > > Better: when you have many semantically-different values, use named
>> > > (not positional) parameters in the format string. […]
>> > >
>> > > <URL:https://docs.python.org/3/library/string.html#formatstrings>
>> >
>> > Except the poster is not using Python 3, so all of this is for naught.
>>
>> Everything I described above works fine in Python 2. Any still-supported
>> version has ‘str.format’.
>
> This response is completely unhelpful. The OP is using Python 2, and
> using %-formatting, and so you give a series of examples of using
> str.format, to, what? Confuse matters?
How about we assume good faith and give Ben the benefit of the doubt that he
simply made a minor and trivial misjudgement rather than accusing him of
intentionally trying to confuse matters?
You are correct that the OP can use % formatting with named arguments. Ben
is correct that the OP can also change his code to use str.format. Some
people hate %-formatting and cannot wait to migrate to {}-formatting, and
some people don't.
(For the record, Internet rumours that %-formatting is deprecated are simply
not correct.)
--
Steve
[toc] | [prev] | [next] | [standalone]
| From | Cai Gengyang <gengyangcai@gmail.com> |
|---|---|
| Date | 2016-05-03 04:12 -0700 |
| Message-ID | <944d06f6-4a96-4cf7-96f9-1df082880bdc@googlegroups.com> |
| In reply to | #107768 |
Ok, I got it to work with no error message finally ...
Enter a name: cai gengyang
Enter an adjective: beautiful
Enter a second adjective: honest
Enter a third adjective: pretty
Enter a verb: hit
Enter a second verb: run
Enter a third verb: jump
Enter a noun: honesty
Enter a noun: patience
Enter a noun: happiness
Enter a noun: danger
Enter an animal: elephant
Enter a food: burger
Enter a fruit: watermelon
Enter a number: 1985
Enter a superhero_name: batman
Enter a country: america
Enter a dessert: icekachang
Enter a year: 1984
This morning I woke up and felt
because _ was going to finally h
e big _ honest. On the other sid
onesty were many patiences prote
eep elephant in stores. The crow
_ to the rythym of the burger,
all of the runs very _. happine
o _ into the sewers and found wa
ats. Needing help, pretty quickl
ump. 1985 appeared and saved cai
by flying to batman and dropping
puddle of america. icekachang th
leep and woke up in the year 198
rld where dangers ruled the worl
$
On Thursday, April 28, 2016 at 3:25:46 PM UTC+8, Steven D'Aprano wrote:
> On Thursday 28 April 2016 17:08, Stephen Hansen wrote:
>
> > On Wed, Apr 27, 2016, at 11:55 PM, Ben Finney wrote:
> >> Stephen Hansen <me@ixokai.io> writes:
> >>
> >> > On Wed, Apr 27, 2016, at 10:32 PM, Ben Finney wrote:
> >> > > Better: when you have many semantically-different values, use named
> >> > > (not positional) parameters in the format string. [...]
> >> > >
> >> > > <URL:https://docs.python.org/3/library/string.html#formatstrings>
> >> >
> >> > Except the poster is not using Python 3, so all of this is for naught.
> >>
> >> Everything I described above works fine in Python 2. Any still-supported
> >> version has 'str.format'.
> >
> > This response is completely unhelpful. The OP is using Python 2, and
> > using %-formatting, and so you give a series of examples of using
> > str.format, to, what? Confuse matters?
>
> How about we assume good faith and give Ben the benefit of the doubt that he
> simply made a minor and trivial misjudgement rather than accusing him of
> intentionally trying to confuse matters?
>
> You are correct that the OP can use % formatting with named arguments. Ben
> is correct that the OP can also change his code to use str.format. Some
> people hate %-formatting and cannot wait to migrate to {}-formatting, and
> some people don't.
>
> (For the record, Internet rumours that %-formatting is deprecated are simply
> not correct.)
>
>
>
> --
> Steve
[toc] | [prev] | [next] | [standalone]
| From | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| Date | 2016-04-28 17:17 +1000 |
| Message-ID | <mailman.180.1461827876.32212.python-list@python.org> |
| In reply to | #107758 |
Stephen Hansen <me@ixokai.io> writes: > On Wed, Apr 27, 2016, at 11:55 PM, Ben Finney wrote: > > Everything I described above works fine in Python 2. > > This response is completely unhelpful. I'll let the OP be the judge of that. > The OP is using Python 2, and using %-formatting, and so you give a > series of examples of using str.format, to, what? Confuse matters? I don't know why you keep raising Python 2 as though it matters. As I pointed out, everything I discussed works fine on Python 2 without any change. More to the point, *even if* they continue using ‘%’ formatting, the primary point was to use named parameters. That is relevant to Python 3 and Python 2; it is relevant to ‘%’ formatting and ‘str.format’ formatting. None of those need to change. So I encourage the OP to try using named format parameters in *any* of those, and then judge whether it is helpful. -- \ “Begin with false premises and you risk reaching false | `\ conclusions. Begin with falsified premises and you forfeit your | _o__) authority.” —Kathryn Schulz, 2015-10-19 | Ben Finney
[toc] | [prev] | [next] | [standalone]
| From | Stephen Hansen <me+python@ixokai.io> |
|---|---|
| Date | 2016-04-27 21:55 -0700 |
| Message-ID | <mailman.173.1461819362.32212.python-list@python.org> |
| In reply to | #107755 |
On Wed, Apr 27, 2016, at 09:37 PM, Cai Gengyang wrote: > print STORY % (Adjective1, name, Verb1, Adjective2, Noun1, Noun2, animal, > food, Verb2, Noun3, fruit, Adjective3, name, Verb3, number, name , > superhero_name, superhero_name, name, country, name, dessert, name, year, > Noun4) Python is case-sensitive. "Adjective1" and "adjective1" are separate things. In your code you're reading into "adjective1". -- Stephen Hansen m e @ i x o k a i . i o
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web