[Tutor] how to accept an integer?
Jerry Hill
malaclypse2 at gmail.com
Wed Dec 5 22:16:46 CET 2007
On Dec 5, 2007 4:01 PM, Mahesh N <mahesh.mach at gmail.com> wrote:
> I dun understand the mistake. My aim is to accept an integer number. The
> python lookup in IDLE asks for a string object but the interpreter returns
> with the following error message. Some one pls explain.
> Thank You
>
> PS : I understand that i can do type conversion after getting input thru
> raw_input(). But how does input() function work?
>
> >>> prompt="temme a number\n"
> >>> speed =input(prompt)
>
> Traceback (most recent call last):
> File "<pyshell#56>", line 1, in <module>
> speed =input(prompt)
> TypeError: 'str' object is not callable
You have code that you haven't shown us. My crystal ball tells me
that somewhere above this point you did input = "Some String", thus
shadowing the builtin input function. Start a new interpreter and try
again and you should find that it works as expected.
As I'm sure you'll hear from others, it really is best to use
raw_input instead. If you want an integer instead of a string, do
something like this:
speed = int(raw_input(prompt))
That way whatever the user types isn't run as python code, just read
in as a string and then converted into an integer.
--
Jerry
More information about the Tutor
mailing list