[Tutor] exercise problem

Francesco Loffredo fal at libero.it
Fri Aug 27 15:55:19 CEST 2010

On 27/08/2010 12.23, Roelof Wobben wrote:
> ------------------------------------------------------------------------
> From: rwobben at hotmail.com
> To: alan.gauld at btinternet.com
> Subject: RE: [Tutor] exercise problem
> Date: Fri, 27 Aug 2010 07:04:39 +0000
>  > To: tutor at python.org
>  > From: alan.gauld at btinternet.com
>  > Date: Thu, 26 Aug 2010 23:54:19 +0100
>  > Subject: Re: [Tutor] exercise problem
>  >
>  > "Roelof Wobben" <rwobben at hotmail.com> wrote
>  >
>  > > Write a function add_vectors(u, v) that takes two lists of numbers
>  >
>  > > I think that u is the name of the new list and v is the number which
>  > > represent the number which must be eveluated.
>  >
>  > No. It sounds like you don't really understand the basic concepts
>  > behind functions yet. it does for me! :-)
>  > ...
>  > --
>  > Alan Gauld
>  > Author of the Learn to Program web site
>  > http://www.alan-g.me.uk/
> Hello,
> I read your page and I think I understand the basic concepts.
> What I don't see is what s and v represent.
> My new idea is that u is the number which must be calculated and v is
> the vector which containts the outcome or u is the outcome of the first
> numbers and v the outcome of the second numbers.
> Roelof
Ok, let's take a deep breath and start from the beginning:

First: a vector is a (usually small) ordered set of numbers that are 
taken together to represent some mathematical entity or physical 
quantity. For example, (3,1,7) can mean the position of an object in 
space, relative to a Cartesian 3-dimensional axis system.

Second: the sum of two vectors is defined as a new vector, whose 
coordinates (the elements of the vectors) are each the sum of the same 
coordinates of the two given vectors:
v1 = (a, b, c)
v2 = (x, y, z)
v1 + v2 = (a+x, b+y, c+z)

That said, Alan tried to point you to the very important concept that:
Third: a function usually produces a result in itself, using the values 
given as arguments (those inside the parentheses, in your case u and v).
And that result is usually assigned to a variable, or used directly, by 
the funcion call itself:
new_vector = add_vectors(u, v)
print add_vectors(u, v)

The function you want to write should sum two of these vectors, that can 
be represented in Python as tuples (or as lists, as your exercise told you):
first_vector = (3, 1, 7)
second_vector = (7, -1, 13)
result = add_vectors(first_vector, second_vector)
print result
(10, 0, 20)

So, if you are asked to write a function that "takes two lists of 
numbers", I thought that those two lists were vectors, called u and v, 
and that the result of the sum, the "new vector", would be the result 
produced by the function.

Hope this made it clearer
-------------- next part --------------

Nessun virus nel messaggio in uscita.
Controllato da AVG - www.avg.com 
Versione: 9.0.851 / Database dei virus: 271.1.1/3096 -  Data di rilascio: 08/26/10 20:34:00

More information about the Tutor mailing list