On 27/08/2010 17.05, Roelof Wobben wrote:
Hello,

My first try :

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=1
getal1=0
getal2=0
while teller < len(u):

Up to this point, it's a good start.

Let's address the last problem first:
> uitkomst = add_vectors[u,v]
>
> But now I get this error message :
>
> uitkomst = add_vectors[u,v]
>
> TypeError: 'function' object is not subscriptable
>
> So it seems that I can't use vectors as a variable in a function.

No, it just seems that you used brackets [] instead of parentheses () in the function call. When you call a function, you must follow its name with parentheses, even when the function doesn't need arguments.
So this line should read:
uitkomst = add_vectors(u, v)
or, if you really (really?) think the two vectors should be enclosed in a bigger list:
uitkomst = add_vectors([u, v]).
This is just one of the problems, I'll address the rest when you'll find them. Just some hints:

getal1 = getal1 + u[teller,0] + v[teller,0] ???
getal2 = getal2 + v[teller,1] + v[teller,1] ???
> teller=teller+1

Ok, while I was writing, Walter told you about parentheses, so I'll address your next question:

But now Im getting this message :


Traceback (most recent call last):

File "C:\Users\wobben\workspace\oefeningen\src\test.py", line 28, in <module>

uitkomst = add_vectors(u,v)

File "C:\Users\wobben\workspace\oefeningen\src\test.py", line 17, in add_vectors

getal1 = getal1 + u[teller,0] + v[teller,0]
TypeError: list indices must be integers, not tuple

When you call add_vectors, you give it u and v as arguments, and they are two lists, as per your requirements:
v = vector[0]  makes  v equal to the list [1, 0], and
u = vector[1]  makes  u equal to the list [1, 1].
Inside add_vectors, you want to sum the coordinates of the two vectors, so you should refer to each of them with their position in the list. Just as you did with the list called vector, you should use a single integer to address an element also in the u and v lists inside add_vectors. If you use a couple of numbers separated by a comma, as you did, Python reads them as a tuple, and it complains. So the 2 lines that sum the vectors should become a single one:

getal1 = getal1 + u[teller] + v[teller]

Then you don't even need to accumulate the sum in the variable getal1, because you are building a list and you can store each sum in a different position of the new list... remember the append method of the lists? So that line should read:

getal1 = u[teller] + v[teller]

and getal1 should be appended to a list... uitkomst2, perhaps?


Roelof

Francesco


return uitkomst2[getal1, getal2] ???
uitkomst= []
vector= [[1,0], [1,1]]
v=vector[0]
u=vector[1]
uitkomst = add_vectors[u,v]

But now I get this error message :

uitkomst = add_vectors[u,v]

TypeError: 'function' object is not subscriptable

So it seems that I can't use vectors as a variable in a function.

Roelof
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
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to