[Tutor] exercise problem
Roelof Wobben
rwobben at hotmail.com
Fri Aug 27 17:33:46 CEST 2010
Hello,
Thanks for pointing this out.
I change it to 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=1
getal1=0
getal2=0
while teller < len(u):
getal1 = getal1 + u[teller,0] + v[teller,0]
getal2 = getal2 + v[teller,1] + v[teller,1]
teller=teller+1
uitkomst2 = [getal1, getal2]
return uitkomst2
uitkomst= []
vector= [[1,0], [1,1]]
v=vector[0]
u=vector[1]
print u, v
uitkomst = add_vectors(u,v)
print uitkomst
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
Roelof
Date: Fri, 27 Aug 2010 16:20:27 +0100
Subject: Re: [Tutor] exercise problem
From: wprins at gmail.com
To: rwobben at hotmail.com
CC: tutor at python.org
Hi Roelof,
See below
On 27 August 2010 16:05, Roelof Wobben <rwobben at hotmail.com> wrote:
uitkomst = add_vectors[u,v]
But now I get this error message :
Traceback (most recent call last):File "C:\Users\wobben\workspace\oefeningen\src\test.py", line 27, in <module>
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.
Carefully compare the syntax for calling your function (as in the doctest) to what you've written above. See the difference? (Hint: check the type of parentheses...)
The error message is giving you a hint -- a subscriptable item is something like a list or array. They use square brackets. Function calls always use (round) parentheses. To python, it looks like you're trying to subcript the function object "add_vectors", which obviously isn't possible. Hence the message.
Regards,
Walter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100827/5d7bfb39/attachment.html>
More information about the Tutor
mailing list