[Tutor] Raw input query (?)

Dave Angel d at davea.name
Thu Aug 25 12:17:49 CEST 2011


On 08/25/2011 05:46 AM, Lisi wrote:
> I copied and ran the following script:
>
> multiplier = 12
>
> for j in range(1,13):
>      print "%d x %d = %d" %(j, multiplier, j*multiplier)
>
> That ran perfectly and gave me the 12 times table.
>
> I then decided that it would be fun to branch out and make teh
> script "universal", so I wrote and ran:
>
> print "Which times table do you want?"
>
> multiplier = raw_input ()
>
> for j in range(1,13):
>      print "%d x %d = %d" %(j, multiplier, j*multiplier)
>
> The j in range section of code is identical, but I now get:
>
> lisi at Tux:~/Python$ python multiplier.py
> Which times table do you want?
> 4
> Traceback (most recent call last):
>    File "multiplier.py", line 8, in<module>
>      print "%d x %d = %d" %(j, multiplier, j*multiplier)
> TypeError: int argument required
> lisi at Tux:~/Python$
>
> What extra should I have done because the variable value came from the
> keyboard, and why is it different from the first example?
>
> Lisi
>
If you get an error with that specific a wording, use the system to tell 
you what's wrong.  Add a print statement immediately before the 
offending line, and print thorough information about each of the 
variables used.  In this case, the obvious candidate is multiplier.

So print repr(multiplier) and type(multiplier) and see if that shows you 
anything different from what the error message asked for, or different 
from the repr() and type() of your first script.

Now that you know what the type of variable multiplier is, can you guess 
how to convert it to the one you need?

DaveA


-- 

DaveA



More information about the Tutor mailing list