[Tutor] how to accept an integer?

2007-12-05 Thread Mahesh N
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)

Traceback (most recent call last):
  File pyshell#57, line 1, in module
speed =input(temme a number\n)
TypeError: 'str' object is not callable
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to accept an integer?

2007-12-05 Thread Kent Johnson
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:

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

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to accept an integer?

2007-12-05 Thread Eric Brunson
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?

Did you read this?

http://docs.python.org/lib/built-in-funcs.html#l2h-40

What do you not understand about it?


  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)

 Traceback (most recent call last):
   File pyshell#57, line 1, in module
 speed =input(temme a number\n)
 TypeError: 'str' object is not callable



 

 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor
   

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to accept an integer?

2007-12-05 Thread Mahesh N
On Dec 6, 2007 2:31 AM, Mahesh N [EMAIL PROTECTED] 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)

 Traceback (most recent call last):
   File pyshell#57, line 1, in module
 speed =input(temme a number\n)
 TypeError: 'str' object is not callable




absolutely, i went up the shell and found out that i had declared a variable
named input and it was shadowing the input() function. i restarted the shell
and now everything's fine.
Thanks Everyone.




-- 
The Place where I Come from,
We Face our Enemies,
If our enemy is Unarmed,
We Offer our SWORD!!!


Courtesy Warrior Within
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to accept an integer?

2007-12-05 Thread Mahesh N
On Dec 6, 2007 2:37 AM, Eric Brunson [EMAIL PROTECTED] wrote:

 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?

 Did you read this?

 http://docs.python.org/lib/built-in-funcs.html#l2h-40

 What do you not understand about it?

 
   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)
 
  Traceback (most recent call last):
File pyshell#57, line 1, in module
  speed =input(temme a number\n)
  TypeError: 'str' object is not callable
 
 
 
  
 
  ___
  Tutor maillist  -  Tutor@python.org
  http://mail.python.org/mailman/listinfo/tutor
 


sorry i dint go thru the docs. i thought i will delve into it once i felt
comfortable with the language.
is the new line character not allowed in input() statement ???
cuz i tried it without the new line character and it works fine.
More over i find python to be a little sluggish after having worked with C
and Java. But one thing's for sure. Its damn powerful and a lovely language.
can someone temme where python is most applicable?
server side scripting? am i guessing it right?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to accept an integer?

2007-12-05 Thread Jerry Hill
On Dec 5, 2007 4:01 PM, Mahesh N [EMAIL PROTECTED] 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
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to accept an integer?

2007-12-05 Thread Kent Johnson
Bryan Fodness wrote:
 speed = int(raw_input(prompt))
 
  
 Is this how ALL known integers should be input?

Yes, with probably a try/except block and a while loop around it to 
handle invalid input.

There are two good reasons for doing this instead of using input:
- it guarantees that the input is in fact an integer (raises ValueError 
otherwise)
- it protects against malicious input (you can do a lot of damage in an 
input(), whatever you type is eval'ed)

For input of unknown integers, you are on your own, Python does not have 
anything built-in for that ;-)

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to accept an integer?

2007-12-05 Thread Jerry Hill
On Dec 5, 2007 4:46 PM, Bryan Fodness [EMAIL PROTECTED] wrote:
 On Dec 5, 2007 4:16 PM, Jerry Hill [EMAIL PROTECTED] wrote:
  speed = int(raw_input(prompt))


 Is this how ALL known integers should be input?

I don't think I understand the question.  If you are prompting your
user to enter an integer from the console, then yes, this is the
general way you should do it, probably wrapped in a try/except block.

You certainly don't have to do it all in one line like that.  Instead,
you could split it up into component parts, like this:

prompt = Please enter a number between 1 and 10:\n
user_input = raw_input(prompt)
try:
user_number = int(user_input)
except ValueError:
print You did not enter a number.

-- 
Jerry
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to accept an integer?

2007-12-05 Thread Alan Gauld

Mahesh N [EMAIL PROTECTED] wrote


 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

This suggests that you have a variable somewhere called input
that is hiding the function. Try doing del(input)
and try it again.

As to what input does; it evaluates whatever string you give
to it so if your user types in some malicious Python code
input will execute it. For that reason input is usually frowned
on as a security risk and int(raw_input()) is preferred.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to accept an integer?

2007-12-05 Thread Alan Gauld
Mahesh N [EMAIL PROTECTED] wrote

 More over i find python to be a little sluggish after having worked 
 with C
 and Java.

If you translate C or Java code into python you will usually
get a less than optimal implementation. Python should be
barely slower than Java and often faster. Compared to
C - yes there is a slow-down.

But even in C you can use tools like Psycho and Pyrex to
speed up critical sections to near C speeds if the problem fits.
Or rewrite the critical section in C and wrap it as a module
using SWIG. Thats how most of the performance ritical modules
in the library are written. Where the major bottleneck is I/O
work like database disk access or GUI or network sockets
then you should find Python fast enough.

 can someone temme where python is most applicable?
 server side scripting? am i guessing it right?

Python has been used in almost every form of programming
from image processing and database manipulation to games
programming and web server development. Do a search on
Source Forge for projects using Python for an example of
the variety.

I'd avoid operating systems, device drivers and hard real-time
applications though.


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to accept an integer?

2007-12-05 Thread Bryan Fodness
On Dec 5, 2007 4:16 PM, Jerry Hill [EMAIL PROTECTED] wrote:

 On Dec 5, 2007 4:01 PM, Mahesh N [EMAIL PROTECTED] 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))


Is this how ALL known integers should be input?




 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
  ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor




-- 
The game of science can accurately be described as a never-ending insult to
human intelligence. - João Magueijo
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor