[Tutor] exercise problem

Roelof Wobben rwobben at hotmail.com
Fri Aug 27 20:35:21 CEST 2010


 

> Date: Fri, 27 Aug 2010 14:27:34 -0400
> From: davea at ieee.org
> To: rwobben at hotmail.com
> CC: alan.gauld at btinternet.com; tutor at python.org
> Subject: Re: [Tutor] exercise problem
> 
> (Don't top-post, it loses all the context)
> 
> 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.
> > on every loop getal1 has the value of the outcome.
> > So I thought this would work
> >
> > uitkomst2 [teller] = getal1 
> >
> > But then i get a out of range.
> >
> > 
> Where did you put that statement? Was it indented like getal1= and 
> teller= ? I doubt it. If you don't use the value till the loop is 
> over, then the subscript will be wrong, and so will the value.
> 
> The other problem is you're confusing the variables inside the function 
> with the ones declared outside. While you're learning, you should use 
> different names for the two sets of variables. So create a new variable 
> inside the function, called something like result. Give it an initial 
> value, a list of the desired size. Then assign to one of its elements 
> each time through the loop. And don't forget to change the return 
> statement to return that variable instead of the confused-named one.
> 
> DaveA
> 


Hello, 

 

I put in right after getal1 = 

I have tried another solution nl. 

 

Put every outcome in a string.

Then convert the string into a list like 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
    uitkomst=""
    while teller < len(u):
        getal1 = u[teller] + v[teller]
        uitkomst = uitkomst + str(getal1) 
        teller=teller+1
    uitkomst2 = list(uitkomst)
    return uitkomst2
             
uitkomst= []
uitkomst2=[]
vector= [1, 2, 1], [1, 4, 3]
v=vector[0]
u=vector[1]
uitkomst = add_vectors(u,v)
print uitkomst     

 

But then I get a list of string instead of integers.

 

You say I have to make a vector and put all the values into it.

That can work only you have to know how big the vector must be.

Or work with a lot of if then.

 

Roelof

 
 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100827/935b0621/attachment.html>


More information about the Tutor mailing list