[Tutor] how to accept an integer?

Kent Johnson kent37 at tds.net
Wed Dec 5 22:24:48 CET 2007

Mahesh N 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
>  >>> speed =input("temme a number\n")

It looks like you have named a string 'input'; this hides the built-in 
'input' function and causes the error you are seeing. Restart IDLE and 
input() should work correctly.

A safer way to accept an integer is to use raw_input() and convert the 
result yourself:

   speed = int(raw_input(prompt))
except ValueError:
   print 'You did not enter an integer'


More information about the Tutor mailing list