Re: [Tutor] exercise problem
"Dave Angel" wrote while teller < len(u): getal1 = u[teller] + v[teller] uitkomst = uitkomst + str(getal1) But then I get a list of string instead of integers. You're close. Now that you've initialized the result variable to [], you can use + just as you're doing. Just take out the str() function in that line. You've still got duplicate names there between that function and the outer level code. You will need to make the new result a list too for + to work, like so: uitkomst = uitkomst + [getal1] Or you can just append() the answer to uitkomst HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] exercise problem
Roelof Wobben wrote: Date: Fri, 27 Aug 2010 14:27:34 -0400 From: da...@ieee.org To: rwob...@hotmail.com CC: alan.ga...@btinternet.com; tutor@python.org Subject: Re: [Tutor] exercise problem 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're close. Now that you've initialized the result variable to [], you can use + just as you're doing. Just take out the str() function in that line. You've still got duplicate names there between that function and the outer level code. There's also no need to convert uitkomst to a list, since it already is. DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] exercise problem
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 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
Re: [Tutor] exercise problem
> Date: Fri, 27 Aug 2010 14:27:34 -0400 > From: da...@ieee.org > To: rwob...@hotmail.com > CC: alan.ga...@btinternet.com; tutor@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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] exercise problem
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. Roelof Date: Fri, 27 Aug 2010 10:19:30 -0700 From: alan.ga...@btinternet.com Subject: Re: [Tutor] exercise problem To: rwob...@hotmail.com >u v, result first example. u : [1.0] v: [1,1] result [2.1] OK, Great, you got that. first split u en v in only numbers. No, you should leave them as lists. Then add u[0] en v[0] and u[1] and v[1] put the outcome in the new vector. Almost except you don't know how many elements there will be so you need a loop to process all the elements. outcome= [outcome u, outcome[u] return outcome. This confused me, the output should be: [ u[0]+v[0], u[1]+v[1], u[2]+v[2], , [u[n]+v[n] ] And in case you are wondering, a vector is used in math to, for example, represent a point in space. A 2 dimensional point has an X,Y coordinate so we can create a 2 element vector: [x,y] We can add, subtract and multiply vectors. Vectors can also be used to represent other physical measures, for example AC electric current has a magnitude and phase angle at any point in time. These two values can be combined as a vector. We can use bigger vectors to represent, for example the set of inputs to a parallel port printer, so we would have a list of 8 binary values. Once again we can express mathematically the processing of this vector as a function and by applying the function to the vector deermine the expected output for any given input. That could be, for example, an ASCII character for the printer example... They are very important in science and engineering. The tutorial you are following does expect the reader to be quite math literate - it is part of a university comp sci course after all. If you do not have a strong math background you may find some of the others more readable. For example mine( :-) ) does not assume any knowledge of math beyond basic high school level - really basic geometry and arithmetic. Alan G. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] exercise problem
Roelof Wobben wrote on 08/27/2010 12:18:01 PM: > > > > Date: Fri, 27 Aug 2010 12:00:23 -0400 > > From: bgai...@gmail.com > > To: rwob...@hotmail.com > > CC: tutor@python.org > > Subject: Re: [Tutor] exercise problem > > > > I have been reading your posts and responses. I find myself frustrated > > with your lack of understanding of Python fundamentals and the time and > > energy others are putting in that seems to help only a little. > > > > I recommend you take a very basic tutorial, and be sure you understand > > the basic concepts before tackling what seems too hard for you > > > I follow this basic tutorial : http://openbookproject.net/thinkcs/python/english2e/ch09.html > > > > > > Also I encourage you to take each error message and look up the topic. > > In this case > > > > getal1 = getal1 + u[teller,0] + v[teller,0] > > TypeError: list indices must be integers, not tuple > > > > What does this error tell you? > > That the list index must be a integer. > > > > > What is a tuple? What is an integer? Where is there a tuple in this > > expression? What are you trying to do? > > What a tuple is i can't tell you. it's in the next chapter. > A integer is a number. > What Im trying to do is that I want all the first numbers of the vectors are added and all the second numbers. > > So if I have this vector [1.0] [1,1] You seem still confused on how your parameters to your function are working or the assignment in general. You do not have a vector [[1,0] [1,1]] being passed into your function. You are passing in two lists (vectors). The first one 'u' is [1,0] , the second 'v' is [1,1] part of your code below: vector= [[1,0], [1,1]] v=vector[0] u=vector[1] print u, v uitkomst = add_vectors(u,v) if you just print u it will show [0,1] if you just print v it will print [1,1] thus your code: getal1 = getal1 + u[teller,0] + v[teller,0] getal2 = getal2 + v[teller,1] + v[teller,1] is not doing what you thought it would. (the syntax is wrong also). I am guessing you are trying to index a 2D matrix with this. Section 9.18 would show you how to do that in python. However there is no 2D matrix in your function, there are 2 vectors u and v. So to add the vectors you loop over the index numbers (see 9.13) in your tutorial. in this loop add the value referenced by the index in each list to each other (section 9.2), and assign it to your new list at the same index value (9.8). The one thing that would catch you on the assignment part, is that the list you are assigning to must have an element at that index (I didn't see the append function mentioned in your tutorial yet). Therefore you must initialize the list to which you are assigning to make sure it is the same length as the vectors you are adding (9.12 gives a method for this). Hopefully that will get you a bit closer to your answer. I would suggest spending time in this section and make sure you understand what each line is doing, since these concepts are fundamentals in python programming. Use the interactive session in python to quickly see what each command does. Python is great in that it will give you instant feedback. Chris___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] exercise problem
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 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
Re: [Tutor] exercise problem
> Date: Fri, 27 Aug 2010 12:00:23 -0400 > From: bgai...@gmail.com > To: rwob...@hotmail.com > CC: tutor@python.org > Subject: Re: [Tutor] exercise problem > > I have been reading your posts and responses. I find myself frustrated > with your lack of understanding of Python fundamentals and the time and > energy others are putting in that seems to help only a little. > > I recommend you take a very basic tutorial, and be sure you understand > the basic concepts before tackling what seems too hard for you I follow this basic tutorial : http://openbookproject.net/thinkcs/python/english2e/ch09.html > > Also I encourage you to take each error message and look up the topic. > In this case > > getal1 = getal1 + u[teller,0] + v[teller,0] > TypeError: list indices must be integers, not tuple > > What does this error tell you? That the list index must be a integer. > > What is a tuple? What is an integer? Where is there a tuple in this > expression? What are you trying to do? What a tuple is i can't tell you. it's in the next chapter. A integer is a number. What Im trying to do is that I want all the first numbers of the vectors are added and all the second numbers. So if I have this vector [1.0] [1,1] I try to add 1 and 1 and after that I try to add 0 and 1 and put the answer in a new vector. > > Look up list indexing and see if you can solve it yourself, then return > with any questions that arise from this effort. > > The more you do for yourself the faster and better you will learn and we > will be more encouraged to help. > > How does this sound to you? Will you give it a try? Of course I will give it a try. > > -- > Bob Gailer > 919-636-4239 > Chapel Hill NC > Roelof ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] exercise problem
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 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: wpr...@gmail.com To: rwob...@hotmail.com CC: tutor@python.org Hi Roelof, See below On 27 August 2010 16:05, Roelof Wobben 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 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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] exercise problem
I have been reading your posts and responses. I find myself frustrated with your lack of understanding of Python fundamentals and the time and energy others are putting in that seems to help only a little. I recommend you take a very basic tutorial, and be sure you understand the basic concepts before tackling what seems too hard for you. Also I encourage you to take each error message and look up the topic. In this case getal1 = getal1 + u[teller,0] + v[teller,0] TypeError: list indices must be integers, not tuple What does this error tell you? What is a tuple? What is an integer? Where is there a tuple in this expression? What are you trying to do? Look up list indexing and see if you can solve it yourself, then return with any questions that arise from this effort. The more you do for yourself the faster and better you will learn and we will be more encouraged to help. How does this sound to you? Will you give it a try? -- Bob Gailer 919-636-4239 Chapel Hill NC ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] exercise problem
Hi Roelof, See below On 27 August 2010 16:05, Roelof Wobben 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 > * > > 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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] exercise problem
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 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: rwob...@hotmail.com To: tutor@python.org Subject: RE: [Tutor] exercise problem Date: Fri, 27 Aug 2010 14:38:23 + 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: st...@pearwood.info > To: tutor@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@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] exercise problem
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: st...@pearwood.info > To: tutor@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@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
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@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] exercise problem
On 27/08/2010 12.23, Roelof Wobben wrote: From: rwob...@hotmail.com To: alan.ga...@btinternet.com Subject: RE: [Tutor] exercise problem Date: Fri, 27 Aug 2010 07:04:39 + > To: tutor@python.org > From: alan.ga...@btinternet.com > Date: Thu, 26 Aug 2010 23:54:19 +0100 > Subject: Re: [Tutor] exercise problem > > "Roelof Wobben" wrote > > > Write a function add_vectors(u, v) that takes two lists of numbers > > > I think that u is the name of the new list and v is the number which > > represent the number which must be eveluated. > > No. It sounds like you don't really understand the basic concepts > behind functions yet. it does for me! :-) > ... > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ Hello, I read your page and I think I understand the basic concepts. What I don't see is what s and v represent. 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. Roelof Ok, let's take a deep breath and start from the beginning: First: a vector is a (usually small) ordered set of numbers that are taken together to represent some mathematical entity or physical quantity. For example, (3,1,7) can mean the position of an object in space, relative to a Cartesian 3-dimensional axis system. Second: the sum of two vectors is defined as a new vector, whose coordinates (the elements of the vectors) are each the sum of the same coordinates of the two given vectors: v1 = (a, b, c) v2 = (x, y, z) v1 + v2 = (a+x, b+y, c+z) That said, Alan tried to point you to the very important concept that: Third: a function usually produces a result in itself, using the values given as arguments (those inside the parentheses, in your case u and v). And that result is usually assigned to a variable, or used directly, by the funcion call itself: new_vector = add_vectors(u, v) or print add_vectors(u, v) The function you want to write should sum two of these vectors, that can be represented in Python as tuples (or as lists, as your exercise told you): first_vector = (3, 1, 7) second_vector = (7, -1, 13) result = add_vectors(first_vector, second_vector) print result (10, 0, 20) So, if you are asked to write a function that "takes two lists of numbers", I thought that those two lists were vectors, called u and v, and that the result of the sum, the "new vector", would be the result produced by the function. Hope this made it clearer Francesco 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
Re: [Tutor] exercise problem
From: rwob...@hotmail.com To: alan.ga...@btinternet.com Subject: RE: [Tutor] exercise problem Date: Fri, 27 Aug 2010 07:04:39 + > To: tutor@python.org > From: alan.ga...@btinternet.com > Date: Thu, 26 Aug 2010 23:54:19 +0100 > Subject: Re: [Tutor] exercise problem > > "Roelof Wobben" wrote > > > Write a function add_vectors(u, v) that takes two lists of numbers > > > I think that u is the name of the new list and v is the number which > > represent the number which must be eveluated. > > No. It sounds like you don't really understand the basic concepts > behind functions yet. Try reading the Modules and Functions topic > in my tutorial. See if that clarifies things for you. Getting these > basic > concepts right at the beginning is very important, don't try to rush > it. > > If confused by one tutorial reading an alternative explanation can > often help - at least it does for me! :-) > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor Hello, I read your page and I think I understand the basic concepts. What I don't see is what s and v represent. 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. Roelof ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] exercise problem
"Roelof Wobben" wrote Write a function add_vectors(u, v) that takes two lists of numbers I think that u is the name of the new list and v is the number which represent the number which must be eveluated. No. It sounds like you don't really understand the basic concepts behind functions yet. Try reading the Modules and Functions topic in my tutorial. See if that clarifies things for you. Getting these basic concepts right at the beginning is very important, don't try to rush it. If confused by one tutorial reading an alternative explanation can often help - at least it does for me! :-) -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] exercise problem
On 8/26/2010 12:02 PM Roelof Wobben said... Hello, I have this exercise: Lists can be used to represent mathematical vectors. In this exercise and several that follow you will write functions to perform standard operations on vectors. Create a file named vectors.py and write Python code to make the doctests for each function pass. Write a function add_vectors(u, v) that takes two lists of numbers of the same length, and returns a new list containing the sums of the corresponding elements of each. 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] """ add_vectors should pass the doctests above I think that u is the name of the new list and v is the number which represent the number which must be eveluated. No. u,v are the parameters names for the two lists of numbers of the same length. So in the example add_vectors([1, 0], [1, 1]), u will take on the value [1, 0] and v the value [1, 1]. HTH, Emile Is this right or do I mis understood the exercise ? Roelof ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] exercise problem
Hello, I have this exercise: Lists can be used to represent mathematical vectors. In this exercise and several that follow you will write functions to perform standard operations on vectors. Create a file named vectors.py and write Python code to make the doctests for each function pass. Write a function add_vectors(u, v) that takes two lists of numbers of the same length, and returns a new list containing the sums of the corresponding elements of each. 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] """ add_vectors should pass the doctests above I think that u is the name of the new list and v is the number which represent the number which must be eveluated. Is this right or do I mis understood the exercise ? Roelof ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor