**Python code to find bugs (7) **
Code design requirements of the previous period
Problem with monkey eating peaches: The monkey picked off several peaches on the first day, and ate half of it immediately, but it was not enough, and ate one more. The next morning, he ate half of the remaining peaches and ate another one. After that, I ate half and one of the remaining half of the previous day every morning. When I wanted to eat again in the morning of the 10th day, I saw that there was only one peach left. How many were picked on the first day?
code show as below:
Please read the code carefully to find out the bugs!
**Correct answer: **There are 3 bugs in total. (For the answer time, see how many answers are correct?)
(1) Please note that the third parameter of the range() method is -1, indicating that it is a reverse (or descending) for loop. The number of cycles is determined by the first two parameters. Because the second parameter is 0, the size of the first parameter determines the number of cycles. So, how many times should it loop? Because the result we need to calculate is based on the number of peaches the monkey saw in the morning on the 10th day, not the number left after eating on the 10th day, so according to the rules agreed in the title, the monkey actually only ate for 9 days. Instead of 10 days. Therefore, the number of loops of the algorithm should be 9 instead of 10.
(2) The second obvious problem is the use of the print() method. Once again, this is the Python3 version, so parentheses () must be included.
(3) The third bug seems to be a "typo". It is the second line of the code block of the for loop, x1 = x2, who understands this sentence? From a grammatical perspective, it is an assignment statement that assigns the value of x2 to x1. Didn’t the above line of code just calculate x1? Doesn't it calculate the number of peaches the day before after the cycle? Why assign x2 to x1 again? What are the consequences of it? ... Haha! In this way, it locks x1 equal to 1, so no matter how you loop and calculate, eventually x1==1. It seems that it must have made a mistake. Then let's think about it. After the first cycle, we calculated the number of peaches on the second day from the last day x1 through the number of peaches on the last day x2=1. Then, if we recycle again, we will still use the number of peaches of the next day to calculate the number of peaches of the previous day. Have you ever thought that the number of peaches of the "next day" at this time is still the original x2? Surely not, how much is it? Obviously the number of x1 just calculated, because the relative position has moved forward one day through the reverse cycle, OK, did you suddenly think of something? Yes, immediately after x1=(x2+1)*2, it should be x2=x1, and assign the new value of x1 to x2 at the same time. Haha, that's the case, that x1=x2 was written in reverse? ! Ashamed, ashamed! Typographical errors, clerical errors! How could there be such a clerical error? Don’t know that the reverse is not the same thing at all? Really thought it was algebra? Is it because my girlfriend broke up? So, on the importance of programmers having a stable girlfriend...hey, it's not easy!
To be honest, programmers are sometimes not afraid of logic errors when writing code, because the second derivation can be found, and they are not afraid of syntax errors, because the editor will help us find it. I'm afraid of this kind of weird "typographical error", so I am confused when I read it. I don't know why, but I don't dare to change it casually. I have to study the entire code block clearly and clearly. After it, I feel at ease. I dared to touch this "daddy". No, it's not "daddy". Daddy was used to be cheated, so he should dare to touch this "daddy"!
Okay, let’s consider that we have accumulated a little more experience. From now on, buddies should be careful. You can have any bugs, but it is better not to have such "cheating" bugs.
So, the correct code should look like this:
**Comment: The main purpose of finding bugs yesterday: **
(1) Once again, consolidate the knowledge of the range() parameter and learn to use the reverse cycle.
(2) Consolidate the use of the print() method.
(3) Consolidate the usage of assignment statements to prevent arbitrary code.
For students who have difficulty reading the above code, please read and study the "Python Introduction" sent in front of the Gaodu Express, or the "Python Easy Introduction" video course on the Gaodu website.
**Code design requirements for this issue: **Use recursive algorithm to find 5!
demand analysis:
First of all, this is an excellent question. Because recursive algorithm is a commonly used algorithm in programming, and it is also a very important algorithm. Recursive algorithms are used when it comes to finding the shortest path to machine learning. So, it is really important to learn it!
Of course, the recursive algorithm is not complicated, it is just a very simple algorithm. Many people are somewhat afraid of it, not because of how complicated it is, but because it is a bit abstract. Like multidimensional space or nested loops, it's a little bit brainstorming.
So, today we use the recursive algorithm to calculate the factorial of a number. This is a very good case. I suggest you keep it in mind. Once you forget it, you can take a look at it and you will immediately remember what happened. Up.
Okay, so what exactly is a recursive algorithm?
To understand simply, the recursive algorithm is: first we have to create a function, and this function will contain a calculation rule, which can be simply understood as a calculation. The important thing is that part of this calculation is still calling the function itself. The purpose of this is to use that calculation rule (calculation) repeatedly. Having said that, you who are smart will definitely have questions. If you call yourself repeatedly, won't it become an endless loop? That's right! Therefore, it is not enough to just have this formula. The recursive algorithm must have an exit. This exit is determined by the return value of the function. When the return value of the function becomes a specific value, that calculation will no longer call the function, but will give the final calculation result.
code show as below:
**Of course, the above code is buggy. So, where is the bug? **
Find it out, post it in the message, and have the answer tomorrow.
Reminder, pay special attention to the details of the format grammar.
Recommended Posts