**Python code to find bugs (8) **
**The code design requirements of the previous period: **Use recursive algorithm to find 5!
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) The if statement is a conditional judgment statement, and of course it should be a comparison expression, not an assignment statement, so if j=0 should be changed to: if j == 0.
(2) The second issue is the obvious use of the print() method. The Python3 version must have parentheses ().
(3) The third bug, we have to have a good chat:
For students who are not familiar with recursive algorithms, we need to figure out what exactly should the function return? It's definitely not just a variable. In this example, it seems that the j variable is a formal parameter of a function. To call a function, you need to provide an actual parameter to assign a value to the formal parameter j. So, go with the flow, return j seems to be exactly what the function needs. My friend, I have to remind you kindly, this is a smoke screen!
Excuse me! What should it be?
First of all, another basic common sense is that the actual parameter assigned to the formal parameter j does not have to be a variable named j. Therefore, whether you should return j depends on what the recursive algorithm here should return.
We say that in order to implement a recursive algorithm, the return value must have two premises: First, it is the result of an algorithm (arithmetic formula), and this algorithm includes a call to the function again. Second, this result is dynamic. When the recursive iterative operation is completed, it will become a specific value, triggering the judgment condition, and not calling the function repeatedly.
OK, so it’s obvious. After return, it should not be j but c. Most of the time, c is an expression of factorial operation, which includes another call to the function until j==0. The conditional branch statement is established and c=1 is obtained. At this time, return c is executed instead of fact(0), so that a 54321 factorial expression is completely calculated, and its result is still Assign a value to c and return it as the final return value of the function.
The following figure demonstrates the recursive logic of the recursive algorithm, you can refer to:
So, the correct code should look like this:
**Comment: **The main purpose of finding bugs yesterday:
(1) Learn recursive algorithms.
(2) Consolidate the if conditional judgment statement.
(3) Remind the usage of print() in Python3 again.
For students who have difficulty reading the above code, please read and study the "Introduction to Python" sent by Gaodu, or the "Introduction to Python" video course on the Gaodu website.
**Code design requirements for this issue: **
There are 5 people sitting together and ask how old is the fifth person? He said he was 2 years older than the fourth person. Asked the age of the fourth person, he said that he was 2 years older than the third person. Ask the third person and say that he is two years older than the second person. Ask the second person and say that he is two years older than the first person. Finally, I asked the first person, he said he was 10 years old. How old is the fifth person?
**Requires calculation using recursive algorithm. **
OK, this is a review case of recursive algorithm, which is very helpful for us to consolidate the learning of recursive algorithm.
code show as below:
Of course, the above code is still buggy. Please find out, post in the message, and have the answer tomorrow.
**The correct answer will be announced tomorrow. **
Reminder: Conventionally, all codes are based on Pythpn3.
Recommended Posts