anonymous
  • anonymous
Exercise 1.15 - Buggy loop. Anyone can explain the bugs in the code and give solution example?
MIT 6.189 A Gentle Introduction to Programming Using Python (OCW)
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
jamiebookeater
  • jamiebookeater
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
e.mccormick
  • e.mccormick
n=10 i=10 while i>0: print i if i % 2 ==0: i=i/2 else: i=i+1 That one? Well, look at the condition for stopping. \(i\le0\) would drop out of the loop. Why might that not happen? Here is a clue: What if I changed it to i=10.0? Why is that different? What does it do to the math? The diffeence is partially related to the answer.
anonymous
  • anonymous
Yes, that assignment. If change i=10 to i=10.0, it is resulting unlimited looping, the different just the shown result, it changes from 1,2,1,2... to 1.0,2.0,1.0,2.0....
e.mccormick
  • e.mccormick
That is true... but I wanted to point out what the modulus part does. You never see anything in wither integer or floating point mode that approaches an i of 0, right?

Looking for something else?

Not the answer you are looking for? Search for more explanations.

More answers

e.mccormick
  • e.mccormick
You ask for a solution example... and there simply is not one. This is not intended to be part of working code. It is just there to show how you can make an infinite loop if you are not careful. The "solution" is that when you write a full program or useful piece of code, you test to make sure the loops exit!
anonymous
  • anonymous
OK. I got, it. First, I change the last line to i = i - 1, to make loop works. Then, I assume this code is designed to print 10, 9, 8,...1 in loop.
e.mccormick
  • e.mccormick
Well, it would not be intended to count down because it uses division. But if you wanted code to do half steps to count down by halfs, and then add one if you did not have an even number, it could do that if you do your change of i=i-1.
anonymous
  • anonymous
I'm also busy with the Buggy loop at the moment. I think the easiest is to change while i>0 to while i>1. Then the code will do exactly the same thing, it will just stop at 1.
e.mccormick
  • e.mccormick
@sune Another issue to think about is why is n there? They put n=10 in the code for a reason. What was it?
anonymous
  • anonymous
@e.mccormick That is a very good question. I just ignored n in my first try. I've relooked at it and changed the else statement to: i=n+1. Then when i becomes 5, it stays 5 and n decreases with 1 each time.
e.mccormick
  • e.mccormick
I think that the purpose of the buggy code problem is to show you that there are lots of things it could have intended, and that there is no way to pick the right thing because the code is very vague. A couple comments in there anywhere and the intent would have been much clearer.
anonymous
  • anonymous
Yeah, as @e.mccormick notes, the intent of the programmer is difficult to judge when there are bugs and the comments are inadequate. So, while it may be difficult to "State what you think Ben was trying to do ...", if you've come up with a number of possibilities, and fixes for the bugs based on each of those possibilities, you've learned some useful stuff. Poor Ben ;(

Looking for something else?

Not the answer you are looking for? Search for more explanations.