[Tutor] exercise problem

Roelof Wobben rwobben at hotmail.com
Fri Aug 27 17:05:43 CEST 2010


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):
        getal1 = getal1 + u[teller,0] + v[teller,0]
        getal2 = getal2 + v[teller,1] + v[teller,1]
        teller=teller+1
    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 :

 

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.
 
Roelof
 
 
 

 


 


From: rwobben at hotmail.com
To: tutor at python.org
Subject: RE: [Tutor] exercise problem
Date: Fri, 27 Aug 2010 14:38:23 +0000




Oke, 
 
That's also the point Alan is making.
I try now to make  the function and puttting it on this maillist if it's ready.
 
Maybe I can learn more about efficient progamming or better way to do this.
 
Roelof

 
> From: steve at pearwood.info
> To: tutor at python.org
> Date: Sat, 28 Aug 2010 00:15:15 +1000
> Subject: Re: [Tutor] exercise problem
> 
> On Fri, 27 Aug 2010 08:23:06 pm Roelof Wobben wrote:
> > > > Write a function add_vectors(u, v) that takes two lists of
> > > > numbers
> [...]
> > 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. 
> 
> If you had a function called "add_numbers(x, y)", you would say that x 
> and y are the numbers which are to be added. If you have a function 
> called "add_vectors(u, v)", then u and v are the vectors to be added. 
> Perhaps this example will help:
> 
> 
> u = [1, 10, 100]
> v = [2, 11, 111]
> add_vectors(u, v)
> => [3, 21, 211]
> 
> 
> 
> 
> -- 
> Steven D'Aprano
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100827/2ea8fa61/attachment-0001.html>


More information about the Tutor mailing list