Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #41690 > unrolled thread
| Started by | leonardo selmi <l.selmi@icloud.com> |
|---|---|
| First post | 2013-03-22 18:06 +0100 |
| Last post | 2013-03-24 16:35 -0700 |
| Articles | 11 — 7 participants |
Back to article view | Back to comp.lang.python
how does the % work? leonardo selmi <l.selmi@icloud.com> - 2013-03-22 18:06 +0100
Re: how does the % work? John Gordon <gordon@panix.com> - 2013-03-22 17:14 +0000
Re: how does the % work? Rick Johnson <rantingrickjohnson@gmail.com> - 2013-03-22 11:06 -0700
Re: how does the % work? Tim Roberts <timr@probo.com> - 2013-03-22 21:29 -0700
Re: how does the % work? Rick Johnson <rantingrickjohnson@gmail.com> - 2013-03-23 00:04 -0700
Re: how does the % work? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-23 07:38 +0000
Re: how does the % work? Rick Johnson <rantingrickjohnson@gmail.com> - 2013-03-23 00:57 -0700
Re: how does the % work? Michael Torrie <torriem@gmail.com> - 2013-03-23 09:57 -0600
Re: how does the % work? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-23 23:53 +0000
Re: how does the % work? leonardo <tampucciolina@libero.it> - 2013-03-23 18:05 +0100
Re: how does the % work? Tim Roberts <timr@probo.com> - 2013-03-24 16:35 -0700
| From | leonardo selmi <l.selmi@icloud.com> |
|---|---|
| Date | 2013-03-22 18:06 +0100 |
| Subject | how does the % work? |
| Message-ID | <mailman.3612.1363971987.2939.python-list@python.org> |
hi guys
i wrote this example :
name = raw_input("What is your name?")
quest = raw_input("What is your quest?")
color = raw_input("What is your favorite color?")
print """Ah, so your name is %s, your quest is %s,
and your favorite color is %s.""" % (name, quest, color)
but i get this error: Traceback (most recent call last):
File "/Users/leonardo/print.py", line 5, in <module>
favourite color is %s.''') % (name, quest, color)
TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple'
how can i solve it?
thanks!
[toc] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2013-03-22 17:14 +0000 |
| Message-ID | <kii3i2$e9n$1@reader2.panix.com> |
| In reply to | #41690 |
In <mailman.3612.1363971987.2939.python-list@python.org> leonardo selmi <l.selmi@icloud.com> writes:
> hi guys
> i wrote this example :
> name = raw_input("What is your name?")
> quest = raw_input("What is your quest?")
> color = raw_input("What is your favorite color?")
> print """Ah, so your name is %s, your quest is %s,
> and your favorite color is %s.""" % (name, quest, color)
> but i get this error: Traceback (most recent call last):
> File "/Users/leonardo/print.py", line 5, in <module>
> favourite color is %s.''') % (name, quest, color)
> TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple'
> how can i solve it?
Are you sure you've given us the exact code? The printed text doesn't
exactly match. ("favorite" vs. "favourite", three double-quotes vs
three single-quotes, etc.)
The error message also says that the print statement has a
close-parenthesis after the string and before the arguments, which may be
real problem.
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
[toc] | [prev] | [next] | [standalone]
| From | Rick Johnson <rantingrickjohnson@gmail.com> |
|---|---|
| Date | 2013-03-22 11:06 -0700 |
| Message-ID | <6a09ed34-7c73-4ea1-82da-f6bc99d47e8a@googlegroups.com> |
| In reply to | #41690 |
On Friday, March 22, 2013 12:06:18 PM UTC-5, leonardo selmi wrote:
> hi guys
>
> i wrote this example :
>
> name = raw_input("What is your name?")
> quest = raw_input("What is your quest?")
> color = raw_input("What is your favorite color?")
>
> print """Ah, so your name is %s, your quest is %s,
> and your favorite color is %s.""" % (name, quest, color)
>
> but i get this error: Traceback (most recent call last):
> File "/Users/leonardo/print.py", line 5, in <module>
> favourite color is %s.''') % (name, quest, color)
> TypeError: unsupported operand type(s) for %: 'NoneType'
> and 'tuple'
>
> how can i solve it?
I would advise as your first course of action to invoke your own problem solving skills. Let's break your code down into two distinct parts.
1. ask the user three questions
2. pass the three responses into a string formatting operation.
Anytime you have inputs that are breaking some functionality (and I'm not sure if these inputs ARE breaking anything), then you need to *test* those inputs before just *blindly* passing them off to other code. This means you need to do two tests.
1. Find out what possible responses could be a result of raw_input. This is difficult, because the user could enter anything, even NOTHING. Remember: "Fools are too cleaver!". Normally user input requires some sort of validation. If you just echoing the input, probably not, but in the real world you will need to verify that you are not dealing with a loony.
2. Inject some known values into the string format operation to insure your syntax and expectations are correct.
But most importantly, run the code and observe the Exception message. The exception was a gift from the creators of Python, just as pain is a gift from your creator.
[toc] | [prev] | [next] | [standalone]
| From | Tim Roberts <timr@probo.com> |
|---|---|
| Date | 2013-03-22 21:29 -0700 |
| Message-ID | <7qbqk8p64dp4v2qvgfogvigi91gjdt5k3r@4ax.com> |
| In reply to | #41690 |
leonardo selmi <l.selmi@icloud.com> wrote:
>
>i wrote this example :
>
>name = raw_input("What is your name?")
>quest = raw_input("What is your quest?")
>color = raw_input("What is your favorite color?")
>
>print """Ah, so your name is %s, your quest is %s,
>and your favorite color is %s.""" % (name, quest, color)
No, you didn't. You wrote:
print('''Ah, so your name is %s, your quest is %s, and your
favorite color is %s.''') % (name, quest, color)
>but i get this error: Traceback (most recent call last):
> File "/Users/leonardo/print.py", line 5, in <module>
> favourite color is %s.''') % (name, quest, color)
>TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple'
>
>how can i solve it?
You are using Python 3. In Python 3, "print" is a function that returns
None. So, the error is exactly correct. To fix it, you need to have the %
operator operate on the string, not on the result of the "print" function:
print('''Ah, so your name is %s, your quest is %s, and your
favorite color is %s.''' % (name, quest, color))
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.
[toc] | [prev] | [next] | [standalone]
| From | Rick Johnson <rantingrickjohnson@gmail.com> |
|---|---|
| Date | 2013-03-23 00:04 -0700 |
| Message-ID | <638fe2c5-0b77-4236-9592-b8c0d76ce5da@googlegroups.com> |
| In reply to | #41714 |
On Friday, March 22, 2013 11:29:48 PM UTC-5, Tim Roberts wrote: > You are using Python 3. In Python 3, "print" is a function that returns > None. So, the error is exactly correct. Wait a second... if he is in-fact using Python 3, then why did the call to a non-existent function named "raw_input" not throw an error first? Not to mention THREE calls to a non-existent function named "raw_input"! Hmm, my guess is that the python interpreter was too busy gossiping about that overblown sexual harassment BS at PyCon to even notice.
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-03-23 07:38 +0000 |
| Message-ID | <514d5bef$0$30001$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #41714 |
On Fri, 22 Mar 2013 21:29:48 -0700, Tim Roberts wrote:
> leonardo selmi <l.selmi@icloud.com> wrote:
>>
>>i wrote this example :
>>
>>name = raw_input("What is your name?")
>>quest = raw_input("What is your quest?")
>>color = raw_input("What is your favorite color?")
>>
>>print """Ah, so your name is %s, your quest is %s, and your favorite
>>color is %s.""" % (name, quest, color)
>
> No, you didn't. You wrote:
>
> print('''Ah, so your name is %s, your quest is %s, and your
> favorite color is %s.''') % (name, quest, color)
The difference between those two statements may not be entirely clear to
someone not experienced in reading code carefully.
Consider the difference between:
print(a % b)
print(a) % b
In the first example, the round brackets group the "a % b", which is
calculated first, then printed.
In the second example, in Python 3, the "print(a)" is called first, which
returns None, and then "None % b" is calculated, which raises an
exception.
Just to add confusion, the two lines are exactly the same in Python 2,
where Print is not a function!
> You are using Python 3. In Python 3, "print" is a function that returns
> None. So, the error is exactly correct. To fix it, you need to have
> the % operator operate on the string, not on the result of the "print"
> function:
>
> print('''Ah, so your name is %s, your quest is %s, and your
> favorite color is %s.''' % (name, quest, color))
Exactly correct.
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | Rick Johnson <rantingrickjohnson@gmail.com> |
|---|---|
| Date | 2013-03-23 00:57 -0700 |
| Message-ID | <45a8f2c2-a9e6-4388-96a9-c0a17a25bb7b@googlegroups.com> |
| In reply to | #41716 |
On Saturday, March 23, 2013 2:38:23 AM UTC-5, Steven D'Aprano wrote:
> On Fri, 22 Mar 2013 21:29:48 -0700, Tim Roberts wrote:
> > print('''Ah, so your name is %s, your quest is %s, and your
> > favorite color is %s.''') % (name, quest, color)
>
> The difference between those two statements may not be entirely clear to
> someone not experienced in reading code carefully.
I think the main problem with the code the OP presented is his attempt to stuff a long string literal into the print function. He should have separated the format string from the format operation:
py> fmtstr = '''Ah, so your name is %s, your quest is %s, and your
favorite color is %s.'''
py> print(fmtstr%(1,2,3))
Ah, so your name is 1, your quest is 2, and your
favorite color is 3.
Sorry but i was too lazy to type out three string literal arguments, and instead, I reached for the low hanging syntactical sweetness of the integer peach. Mmm peaches!
[toc] | [prev] | [next] | [standalone]
| From | Michael Torrie <torriem@gmail.com> |
|---|---|
| Date | 2013-03-23 09:57 -0600 |
| Message-ID | <mailman.3644.1364054284.2939.python-list@python.org> |
| In reply to | #41716 |
On 03/23/2013 01:38 AM, Steven D'Aprano wrote:
> Just to add confusion, the two lines are exactly the same in Python 2,
> where Print is not a function!
Perhaps this is a good reason use the slightly more complicated but
easier to get right format method.
print ("{0}, {1}, {2}".format(1,2,3))
And it's worth noting that using the string formatter or the old %
operator on a string works anywhere where there're strings, not just in
the print function.
b = "The answer was %d" % answer
or
b = "The answer was {0}".format(answer)
On the topic of print, I can see the logic in print being a function
now, but I have always preferred it as a print statement; it just looked
cleaner. That was one of the things that initially attracted me to
python. But the reality is print isn't used for much in real programs
other than console debug messages.
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-03-23 23:53 +0000 |
| Message-ID | <514e405c$0$30001$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #41741 |
On Sat, 23 Mar 2013 09:57:48 -0600, Michael Torrie wrote:
> On 03/23/2013 01:38 AM, Steven D'Aprano wrote:
>> Just to add confusion, the two lines are exactly the same in Python 2,
>> where Print is not a function!
>
> Perhaps this is a good reason use the slightly more complicated but
> easier to get right format method.
>
> print ("{0}, {1}, {2}".format(1,2,3))
Misplaced parentheses are possible either way.
print ("{0}, {1}, {2}".format(1,2,3))
print ("{0}, {1}, {2}").format(1,2,3)
> And it's worth noting that using the string formatter or the old %
> operator on a string works anywhere where there're strings, not just in
> the print function.
Very true.
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | leonardo <tampucciolina@libero.it> |
|---|---|
| Date | 2013-03-23 18:05 +0100 |
| Message-ID | <mailman.3649.1364058458.2939.python-list@python.org> |
| In reply to | #41716 |
thank you all!
Il 23/03/2013 8.38, Steven D'Aprano ha scritto:
> On Fri, 22 Mar 2013 21:29:48 -0700, Tim Roberts wrote:
>
>> leonardo selmi <l.selmi@icloud.com> wrote:
>>> i wrote this example :
>>>
>>> name = raw_input("What is your name?")
>>> quest = raw_input("What is your quest?")
>>> color = raw_input("What is your favorite color?")
>>>
>>> print """Ah, so your name is %s, your quest is %s, and your favorite
>>> color is %s.""" % (name, quest, color)
>> No, you didn't. You wrote:
>>
>> print('''Ah, so your name is %s, your quest is %s, and your
>> favorite color is %s.''') % (name, quest, color)
>
> The difference between those two statements may not be entirely clear to
> someone not experienced in reading code carefully.
>
> Consider the difference between:
>
> print(a % b)
>
> print(a) % b
>
> In the first example, the round brackets group the "a % b", which is
> calculated first, then printed.
>
> In the second example, in Python 3, the "print(a)" is called first, which
> returns None, and then "None % b" is calculated, which raises an
> exception.
>
> Just to add confusion, the two lines are exactly the same in Python 2,
> where Print is not a function!
>
>
>> You are using Python 3. In Python 3, "print" is a function that returns
>> None. So, the error is exactly correct. To fix it, you need to have
>> the % operator operate on the string, not on the result of the "print"
>> function:
>>
>> print('''Ah, so your name is %s, your quest is %s, and your
>> favorite color is %s.''' % (name, quest, color))
> Exactly correct.
>
>
>
[toc] | [prev] | [next] | [standalone]
| From | Tim Roberts <timr@probo.com> |
|---|---|
| Date | 2013-03-24 16:35 -0700 |
| Message-ID | <5d3vk8h6tvh0dh22f8ejk570gptdgho7tg@4ax.com> |
| In reply to | #41747 |
leonardo <tampucciolina@libero.it> wrote: > >thank you all! So, what was the problem? -- Tim Roberts, timr@probo.com Providenza & Boekelheide, Inc.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web