Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #41698
| Newsgroups | comp.lang.python |
|---|---|
| Date | 2013-03-22 11:06 -0700 |
| References | <mailman.3612.1363971987.2939.python-list@python.org> |
| Message-ID | <6a09ed34-7c73-4ea1-82da-f6bc99d47e8a@googlegroups.com> (permalink) |
| Subject | Re: how does the % work? |
| From | Rick Johnson <rantingrickjohnson@gmail.com> |
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.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
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
csiph-web