Re: (Learner-here) Lists + Functions = headache
On Mon, May 6, 2013 at 3:39 PM, 8 Dihedral dihedral88...@googlemail.com wrote: Bradley Wright於 2013年5月6日星期一UTC+8上午8時59分15秒寫道: def fizz_cout(x): count = 0 for item in x: while item == fizz: count += 1 return count This is not indented right in the scope to return the total count. Wow. You know a question's been asked frequently when even the bots can learn to answer it correctly. ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: (Learner-here) Lists + Functions = headache
On 06/05/2013 02:30, Bradley Wright wrote: Aha! lessons learned - got it! The next lesson is to read the link given in my signature, digest it and take action to avoid masses of superfluous newlines in your responses. TIA. -- If you're using GoogleCrap™ please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence -- http://mail.python.org/mailman/listinfo/python-list
(Learner-here) Lists + Functions = headache
Hey guys and gals doing this tutorial(codecademy) and needed a bit help from the experienced. I'm writing a function that takes a list(one they supply during runtime) here's what my function is supposed to do 1. for each instance of the string fizz make a count 2. Finally return that count here's my code: def fizz_cout(x): count = 0 for item in x: while item == fizz: count += 1 return count Please remember that i am a eager beginner, where am i going wrong? -- http://mail.python.org/mailman/listinfo/python-list
Re: (Learner-here) Lists + Functions = headache
On May 6, 10:59 am, Bradley Wright bradley.wright@gmail.com wrote: def fizz_cout(x): count = 0 for item in x: while item == fizz: count += 1 return count Please remember that i am a eager beginner, where am i going wrong? There are several problems with your code: for item in x: while item == fizz: count += 1 The `for` takes an item out of the list `x`. If that item is the string 'fizz', it increments count. As it's a `while` loop, it will continue to increment for as long as `item` is 'fizz'. Since the while loop doesn't look up another list item, it will remain as 'fizz' until the end of time. Well, it would except for your second bug: while item == fizz: count += 1 return count The very first time it encounters a list item that is 'fizz', it adds one to `count`, then exits the function passing back `count`. You want to move the return to _outside_ the for loop, and you want to change your `while` condition to an `if` instead. -- http://mail.python.org/mailman/listinfo/python-list
Re: (Learner-here) Lists + Functions = headache
On Sunday, May 5, 2013 9:21:33 PM UTC-4, alex23 wrote: On May 6, 10:59 am, Bradley Wright bradley.wright@gmail.com wrote: def fizz_cout(x): count = 0 for item in x: while item == fizz: count += 1 return count Please remember that i am a eager beginner, where am i going wrong? There are several problems with your code: for item in x: while item == fizz: count += 1 The `for` takes an item out of the list `x`. If that item is the string 'fizz', it increments count. As it's a `while` loop, it will continue to increment for as long as `item` is 'fizz'. Since the while loop doesn't look up another list item, it will remain as 'fizz' until the end of time. Well, it would except for your second bug: while item == fizz: count += 1 return count The very first time it encounters a list item that is 'fizz', it adds one to `count`, then exits the function passing back `count`. You want to move the return to _outside_ the for loop, and you want to change your `while` condition to an `if` instead. Thank you Alex - much appreciated, about to implement right now! -- http://mail.python.org/mailman/listinfo/python-list
Re: (Learner-here) Lists + Functions = headache
On 5/5/2013 8:59 PM, Bradley Wright wrote: Hey guys and gals doing this tutorial(codecademy) and needed a bit help from the experienced. I'm writing a function that takes a list(one they supply during runtime) here's what my function is supposed to do Do they supply an example so you can test both your comprehension and code? I think most specs given in natural language need such. 1. for each instance of the string fizz make a count 2. Finally return that count Did you create an example problem to test your code? If not, do so. Did you run your function with example data? If not, do so. here's my code: def fizz_cout(x): count = 0 for item in x: while item == fizz: count += 1 return count Here is hint as to some of what needs to be improved: this function will return either 1 or None. You should have discovered that by testings. -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: (Learner-here) Lists + Functions = headache
On Sun, 05 May 2013 17:59:15 -0700, Bradley Wright wrote: Hey guys and gals doing this tutorial(codecademy) and needed a bit help from the experienced. I'm writing a function that takes a list(one they supply during runtime) here's what my function is supposed to do 1. for each instance of the string fizz make a count 2. Finally return that count here's my code: def fizz_cout(x): count = 0 for item in x: while item == fizz: count += 1 return count Please remember that i am a eager beginner, where am i going wrong? Lots of places, sorry. The first thing you're doing is hoping that we will guess what error you are getting. In this case, it so happens that we can, but that will not always be the case. You should always give us the code (which you have done), the data it is running on, the expected result, and the actual result. Within reason: don't bombard us with 10,000 lines of code and 3MB of data. Now, moving on to your function: try walking through it yourself in your head. You start off by defining a value, then start iterating over each value in x, one at a time. count = 0 for item in x: Suppose x = [buzz, fizz, buzz, fizz]. Then you should get a result of 2. So far, you start with count = 0. Then you enter the for loop, and item gets the value buzz. The next line enters a while loop: while item == fizz: Since item does *not* equal fizz, the body of the while loop does not run at all, and Python jumps past the while loop, which takes it to the end of the for loop. So Python goes on to the next item. This time item gets set to fizz. So you enter the while loop again, only this time item *does* equal fizz: while item == fizz: count += 1 return count Oh-oh, trouble ahead. But luckily, you have two bugs, and they *almost* cancel themselves out. The first problem is that the while loop would be an infinite loop, going around and around and around over and over again, since you enter it with item == fizz but item always stays equal to fizz. So the first two lines would keep adding one to count, over and over again, until count is so big your computer runs out of memory (and that might take *months*). Fortunately, the very next line *almost* overcomes that bug. It doesn't *fix* it, but it does reduce the severity. After adding one to count the very first time, Python hits the line return count, which immediately exits the function, jumping out of the (infinite) while loop and the for- loop. So your function always returns either 0 (if there are no fizz in the list at all) or 1 (if there is any fizz). So, you have two problems, and they both need to be fixed: 1) The return count line must not happen until the for-loop has completed looking at each item in the list. So it must be outside the for- loop, not inside it. Remember that Python decides what is inside the loop by its indentation. 2) You don't want an infinite loop inside the for-loop. There is no need to have two loops at all. The outer for-loop is sufficient. You look at each item *once*, not over and over again, and decide *once* if you should add one to count, then go on to the next item. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: (Learner-here) Lists + Functions = headache
On Sunday, May 5, 2013 9:24:44 PM UTC-4, Bradley Wright wrote: On Sunday, May 5, 2013 9:21:33 PM UTC-4, alex23 wrote: On May 6, 10:59 am, Bradley Wright bradley.wright@gmail.com wrote: def fizz_cout(x): count = 0 for item in x: while item == fizz: count += 1 return count Please remember that i am a eager beginner, where am i going wrong? There are several problems with your code: for item in x: while item == fizz: count += 1 The `for` takes an item out of the list `x`. If that item is the string 'fizz', it increments count. As it's a `while` loop, it will continue to increment for as long as `item` is 'fizz'. Since the while loop doesn't look up another list item, it will remain as 'fizz' until the end of time. Well, it would except for your second bug: while item == fizz: count += 1 return count The very first time it encounters a list item that is 'fizz', it adds one to `count`, then exits the function passing back `count`. You want to move the return to _outside_ the for loop, and you want to change your `while` condition to an `if` instead. Thank you Alex - much appreciated, about to implement right now! Aha! lessons learned - got it! -- http://mail.python.org/mailman/listinfo/python-list
Re: (Learner-here) Lists + Functions = headache
On Mon, 06 May 2013 01:31:48 +, Steven D'Aprano wrote: So your function always returns either 0 (if there are no fizz in the list at all) or 1 (if there is any fizz). Correction: (thanks to Terry for pointing this out). It will return None or 1, not 0. How easy it is to fall into the trap of assuming the function will do what you intend it to do, instead of what you actually tell it to do :-( -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: (Learner-here) Lists + Functions = headache
Bradley Wright於 2013年5月6日星期一UTC+8上午8時59分15秒寫道: Hey guys and gals doing this tutorial(codecademy) and needed a bit help from the experienced. I'm writing a function that takes a list(one they supply during runtime) here's what my function is supposed to do 1. for each instance of the string fizz make a count 2. Finally return that count here's my code: def fizz_cout(x): count = 0 for item in x: while item == fizz: count += 1 return count This is not indented right in the scope to return the total count. -- http://mail.python.org/mailman/listinfo/python-list