[Tutor] [Fwd: Re: trouble with "if"]

Brian van den Broek broek at cc.umanitoba.ca
Wed May 30 17:21:48 CEST 2007


Adam Urbas said unto the world upon 05/30/2007 11:01 AM:
> I can't exactly show you the error message anymore, because the program is
> now screwed up in so many ways that I can't even get it to do the things it
> used to.
> 
> It says things like ERROR: Inconsistent indentation detected!
> 1) Your indentation is outright incorrect (easy to fix), OR
> 2) Your indentation mixes tabs and spaces.  Then it tells me to untabify
> everything, which i did and it still gives this message.  I've started
> completely over with the exact same indentation and that one works.
> 
> Oh my gosh this gmail is a fricken crack head... none of this stuff was 
> here
> last night.  I have no idea what was going on then, but everything you guys
> said is right here.  The plain text is right next to the Check spelling, 
> the
> reply to all is right above send and save now and in the corner near the
> little arrow.  Well, it's working now.
> 
> Ok, so if i have a section of code that is:
> 
> answer=(2+3):
> print "answer", answer
> 
> so for the code above I would put: (I think I would have to have the two
> numbers and the addition thing in there wouldn't I; I saw something like
> this on Alan's tutorial last night.)
> 
> def answer(2,3):
>    answer=(2+3)
>    print "answer",answer
> 
> That is obviously not right.:
> 
> There's an error in your program:
> invalid syntax
> 
> when it says that it highlights the 2: def answer(2+3):
> 
> Ok I think I understand these now.  Thanks for the advice.  I have this 
> now:
> 
> def answer():
>    print("answer")
> answer()
> 
> It works too, yay!
> Thanks,
> 
> Au
> 


Adam,

Glad you are sorting out the gmail---in the long run, plain text will 
make this all much easier than what you had before :-)

Your answer function definition above is saying something like this: 
make answer the name of a function that takes no parameters, and, when 
called, have it execute a print.

This:

 > def answer(2,3):
 >    answer=(2+3)
 >    print "answer",answer

doesn't work, as you are trying to set the values of the two 
parameters to 2 and 3 in the function definition itself. That's not 
how parameters work. The definition of a function sets the parameters 
up as named `slots' that function calls will give values to. (There 
are, as Andre pointed out, more details, but let those aside for now 
and focus on the simplest cases.)

This:

def answer():
     answer=(2+3)
     print "answer",answer

would work, but it isn't much different than the code that did work.

Try this:

def answer(my_first_parameter, my_second_parameter):
     value = my_first_parameter + my_second_parameter
     print "Answer:\t", value

(I wouldn't use the cumbersome names `my_first_parameter', etc. in 
real code, but perhaps they help keeping track of what is going on in 
early stages.)

That says, in effect, let answer be a function which takes two 
positional parameters, adds them, and prints the result in an 
informative way.

 >>> answer(40, 2)
Answer: 42
 >>> answer("A string", " and another string")
Answer: A string and another string
 >>>

These work because the function definition ensures that the first 
parameter (40, in the first case above) will, as far as the function 
is concerned, be called my_first_parameter. (Likewise for 2 and 
my_second_parameter.)

Does that help?

Brian vdB


More information about the Tutor mailing list