how does the % work?
Rick Johnson
rantingrickjohnson at gmail.com
Fri Mar 22 14:06:26 EDT 2013
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.
More information about the Python-list
mailing list