**Python code to find bugs (9) **
**Code design requirements of the previous period: **
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.
code show as below:
Please read the code carefully to find out the bugs!
**Correct answer: **There are 2 bugs. (For the answer time, see how many answers are correct?)
(1) The fifth line of code: c = age(n) + 2, the age() function is called. This is the characteristic of the recursive algorithm, but the parameter n represents the age in each round of calculation, so it should be n-1 with each call.
(2) Is the return value of the function surely n? Made the same mistake the last time. Should be modified to return c.
So, the correct code should look like this:
**Comment: **This code finds bugs, is a case of reviewing recursive algorithms, the main purpose:
(1) Consolidate the learning recursive algorithm.
(2) Deepen the understanding of the return value of the function in the recursive algorithm.
For students who have difficulty understanding the above code, please read and study the "Introduction to Python" sent by Gaodu, or the video course "Introduction to Python Easy" on the Gaodu website.
**Code design requirements for this issue: **
Two matrices with 3 rows and 3 columns:
X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]
Y = [[5,8,1],
[6,7,3],
[4,5,9]]
Please add the data at the corresponding positions and return a new matrix.
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