[Tutor] this is hard

Gregor Lingl glingl@aon.at
Mon, 19 Aug 2002 22:15:54 +0200


Danny Yoo schrieb:

>On Mon, 19 Aug 2002, melvin terry wrote:
>
>  
>
>>why wont this work? When you type 1 noting happenes
>>
>>#welcome
>>print "welcome ryan I just wanted you to have a second calculator!"
>>print "here you can ahose from what you would like o ind the rarea of..."
>>input = raw_input("type 1 for a rectangle")
>>if input == 1:
>>     heigth = input("please type height here:")
>>     width = input ("please type width here:")
>>     area = height*width
>>     print "The area is", area
>>    
>>
>
>
>Hi Melvin,
>
>
>Can you explain why the program uses raw_input() here:
>
>  
>
>>input = raw_input("type 1 for a rectangle")
>>    
>>
>
>while, later on in the program, it uses the input() function? 
>
moreover, when you try to use the input-function in your program, you 
will not have
a handle to it any more:
When you start the Python-interpreter, input is already there:
 
 >>> input
<built-in function input>
 >>> input = raw_input("type 1 for a rectangle")
type 1 for a rectangle1
 >>> input
'1'
 >>> '1'==1
0

As in Python 0 means false, you see, why the statements within the if - 
statement
are not executed.
But if they were ...

 >>> heigth = input("please type height here:")
Traceback (most recent call last):
  File "<pyshell#4>", line 1, in ?
    heigth = input("please type height here:")
TypeError: 'str' object is not callable
 >>>

... you would get this error. The 'str' object beeing the '1', which has now
overwritten the original input. And als '1' is a string an not a 
function, it cannot
be called:  '1'("please type height here:") simply makes no sense, '1' 
is not
callable.

Remedy:  if this disturbs you, never mind! Simply do never use names (as 
input)
that already have some meaning (i. e. some object they refer to).

Regards, Gregor


> There's a
>difference between the two functions that's causing the bug in the
>program.
>
>
>
>Best of wishes!
>
>
>_______________________________________________
>Tutor maillist  -  Tutor@python.org
>http://mail.python.org/mailman/listinfo/tutor
>
>
>  
>