[Tutor] 'elp!!!!!!!1Totally Clueless Newbie In Distress
Danny Yoo
dyoo at hkn.eecs.berkeley.edu
Fri Jan 19 18:28:00 CET 2007
> I've been dabbling into Python for about 6 weeks now.I'm a Social
> Sciences student who just got interested in programming and chose Python
> as first language.
Out of curiosity, what materials are you using to learn how to program?
> Isn't it legal to start a new block of code when starting a
> definition?And how come it returns 'variable' not defined,when they are
> defined by the = ??Should i make them global?
Wait, wait. I think you may be misunderstanding the use of 'global'.
You should not be using global unless you really need it.
I see three variables here that you are interested in:
altura_aeronave
largura_aeronave
comprimento
Are these always collected together? If they are related, you should have
a single structure that holds them together, rather than represent them as
three separate variables.
Concretely, you can represent these three values as a single tuple. You
can think of it as a "vector" from your mathematics class. For example:
#################################################
def make_measure(start, stop):
"""make_measure: number number -> measure
Creates a new measure from start and stop."""
return (start, stop)
def measure_start(a_measure):
"""measure_start: measure -> number
Selects the start portion of a measure."""
return a_measure[0]
def measure_stop(a_measure):
"""measure_end: measure -> end
Selects the stop portion of a measure."""
return a_measure[1]
#################################################
That is, these functions take inputs and produce outputs. That should be
a concept that you are familiar with from your previous experience:
f(x) = 2x (math notation)
is a function that takes a number and produces the double of that number.
We write this in Python as:
################
def double(x):
"""double: number -> number
Returns the double of x."""
return x * 2
################
Getting back to the measure example: once we have these functions to build
measures and take them apart, we can then use these like this:
################################################
## Small test program
m1 = make_measure(3, 4)
m2 = make_measure(17, 42)
print "m1", measure_start(m1), measure_stop(m1)
print "m2", measure_start(m2), measure_stop(m2)
################################################
If we dislike the duplication of those last two statements here, we can
create a function that doesn't produce an output, but it still takes
input:
########################################################################
def print_measure(header_name, a_measure):
"""print_measure: measure string -> None
Prints out the measurement.
"""
print header_name, measure_start(a_measure), measure_stop(a_measure)
########################################################################
After we define this helper function "print_measure()", our little program
can now look like this:
#########################
## Small test program
m1 = make_measure(3, 4)
m2 = make_measure(17, 42)
print_measure("m1", m1)
print_measure("m2", m2)
#########################
Notice that, here, we do not need to say anything about "globals" to make
effective programs. We are simply passing values back and forth as
parameters.
Does this make sense so far? If you have any questions, please feel free
to ask. Please continue to reply to Tutor by using your email client's
Reply to All feature.
More information about the Tutor
mailing list