[Tutor] exercise problem
Francesco Loffredo
fal at libero.it
Fri Aug 27 20:54:56 CEST 2010
We are close to the solution, keep trying!
On 27/08/2010 19.56, Roelof Wobben wrote:
> Hello,
>
> Now I have this :
>
> def add_vectors(u, v):
> """
> >>> add_vectors([1, 0], [1, 1])
> [2, 1]
> >>> add_vectors([1, 2], [1, 4])
> [2, 6]
> >>> add_vectors([1, 2, 1], [1, 4, 3])
> [2, 6, 4]
> >>> add_vectors([11, 0, -4, 5], [2, -4, 17, 0])
> [13, -4, 13, 5]
> """
> teller=0
> getal1=0
> getal2=0
> while teller < len(u):
> getal1 = u[teller] + v[teller]
> teller=teller+1
> return uitkomst2
>
> uitkomst= []
> uitkomst2=[]
> vector= [1, 2, 1], [1, 4, 3]
> v=vector[0]
> u=vector[1]
> uitkomst = add_vectors(u,v)
> print uitkomst
>
> The only problem I have is to build up uitkomst2.
There is no need to initialize uitkomst, because it will be created by
the line
> uitkomst = add_vectors(u,v)
but you should create the list uitkomst2 INSIDE the add_vectors
function. uitkomst2 is an internal storage for...
> on every loop getal1 has the value of the outcome.
... that's it, for the different values that you put in getal1.
There is no more need for getal2, too.
So you could put the line
> uitkomst2=[]
in the place where you should delete
> getal2=0
that is just before the while loop.
And finally, in the loop, just before
> teller=teller+1
you need to insert the line that you haven't yet read about, but that
you really need: (ta-dah!)
uitkomst2.append(getal1)
As you will (YOU WILL, WON'T YOU?) read in Alan's tutorial, and also in
the one you're reading, this line extends the uitkomst2 list by adding a
new element to its 'tail': the number getal1.
So, if you follow the flow of your function, you can see it happening:
-------------- BEGINNING -----------------------
vector= [1, 2, 1], [1, 4, 3] # maybe Python can forgive you, but
# I would write [[1, 2, 1], [1, 4, 3]]
# or ([1, 2, 1], [1, 4, 3]) ...
v=vector[0] # now v = [1, 2, 1]
u=vector[1] # now u = [1, 4, 3]
uitkomst = add_vectors(u,v) # add_vectors is called to provide a value
teller=0
getal1=0
uitkomst2 = []
while teller < len(u): # teller= 0, len(u) = 3, OK
getal1 = u[teller] + v[teller] # getal1 = u[0]+v[0] = 1+1 = 2
uitkomst2.append(getal1) # uitkomst2 is now [2]
teller=teller+1
while teller < len(u): # teller= 1, len(u) = 3, OK
getal1 = u[teller] + v[teller] # getal1 = u[1]+v[1] = 2+4 = 6
uitkomst2.append(getal1) # uitkomst2 is now [2, 6]
teller=teller+1
while teller < len(u): # teller= 2, len(u) = 3, OK
getal1 = u[teller] + v[teller] # getal1 = u[2]+v[2] = 1+3 = 4
uitkomst2.append(getal1) # uitkomst2 is now [2, 6, 4]
teller=teller+1
while teller < len(u): # teller= 3, len(u) = 3, STOP!
return uitkomst2 # and finally the list uitkomst2 becomes the value
# that the add_vectors function will provide.
uitkomst = add_vectors(u,v) # now uitkomst becomes [2, 6, 4]
print uitkomst # and lived happily ever after.
> So I thought this would work
>
> uitkomst2 [teller] = getal1
>
> But then i get a out of range.
sure, because you tried to access an element in an empty list.
>
> Roelof
Hope you got it, and keep trying!
Francesco
-------------- 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