[Tutor] Help understanding code

Kirk Bailey idiot1 at netzero.net
Mon Oct 20 11:33:07 EDT 2003


OK, how about this?

def square(x):		# define the square function
	return x * x	# return the value of multiplying x by  itself

now in the definition X is an arguement passed to the function when you invoke 
it. For instance:

side=input('What is the sidelength of your square?')
area=square(side)
print 'The surface area of your square courtyard is ', x, 'units.'

notice that I passed a variable called 'side', NOT 'x'. However, in it's 
operation it  used a variable named x, and that variable was local- noplace else 
in the program could access it.

Any help as a beginning?


Lisa Sullivan wrote:

> I am new to programming and I'm trying to teach myself Python via online 
> tutorials.  I am
> 
> struggling with functions.  I wonder if someone could help me.
> Here is the URL for the program I am trying to understand:
> http://www.honors.montana.edu/~jjc/easytut/easytut/node10.html#SECTION001030000000000000000 
> 
> I have worked through the program and I understand all but one line.  
> Below is an edited version
> 
> of my notes while I was working through understanding the program.
> Let me know if I should post the program as well.
> 
>  if get_questions(questions[index]):
> I don't understand this line.
> It seems to move through the list stored in (questions) and determine 
> whether or not to increase
> 
> right by 1.
> 
> Right should only be increased if the question was answered right, so:
> That must be what the return statements in the check_question function 
> are for.
> It replaces the words get_questions with the value returned from running 
> get_questions.
> We set the variable true to equal 1 and the variable false to = 0.
>  if get_questions(questions[index]):
> now could say
> if 1(questions[index]):
> or
> if 0(questions)[index]:
> 
> (questions) refers to the get_questions info sent to the run_test 
> function that we are now inside
> 
> of.
> and [index] refers to an entry in this list.  But which one?
> Is index used to get the index variable we set above?
> If so, then right will only be increased:
> if 0 or 1(questions[current question]
> In other words if the current question is answered correctly or 
> incorrectly.
> This doesn't make sense to me.
> I don't understand how the program is progressing through the questions.
> I don't see how it knows to skip the answers.
> I know it has something to do with the fact that the questions and 
> answers are stored in lists
> 
> that are inside of the main list.
> 
> I hope I'm not too far off here.
> Thanks for your help.
> Lisa
> 
> _________________________________________________________________
> Add MSN 8 Internet Software to your current Internet access and enjoy 
> patented spam control and more.  Get two months FREE!     
> http://join.msn.com/?page=dept/byoa
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> 

-- 

-- 

end

Cheers!
         Kirk D Bailey

  +                              think                                +
   http://www.howlermonkey.net  +-----+        http://www.tinylist.org
   http://www.listville.net     | BOX |  http://www.sacredelectron.org
   Thou art free"-ERIS          +-----+     'Got a light?'-Prometheus
  +                              kniht                                +

Fnord.




More information about the Tutor mailing list