Python code to find bugs(8)

**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

Python code to find bugs (2)
Python code to find bugs(7)
Python code to find bugs(4)
Python code to find bugs(9)
Python code to find bugs(6)
Python code to find bugs (1)
Python code to find bugs(8)
Python code to find bugs(5)
Actually very simple | Python code to find bugs (12)
Minimalism is the soul of Python | Python code to find bugs (10)
How to comment python code
python how to view webpage code
How to wrap in python code
Python handles operation code to execl
01. Introduction to Python
200 lines of Python code to achieve snake
Python Romberg method to find integral examples
Does Python code need to be indented
Introduction to Python
How to use code running assistant in python
500 lines of python code to achieve aircraft war
How to set code auto prompt in python
Python how to move the code collectively right
Centos 6.4 python 2.6 upgrade to 2.7
Centos 6.4 python 2.6 upgrade to 2.7
Python SMS bombing code
Centos default python2.6 upgrade to
Solution to python alignment error
CentOS upgrade python2 to pyth
How to write Pythonic code
Python drawing rose implementation code
How to uninstall python plugin
Introduction to Python related modules
Python regular expression example code
Find the kernel source code corresponding to the running Ubuntu version
Python implements code block folding
Python install OpenCV sample code
3 ways to encrypt Python files
Python implements verification code recognition
How to understand python objects
How to use python tuples
Is python code case sensitive
Python telnet login function implementation code
Python| function using recursion to solve
Method steps to increase python font
How to use python thread pool
Use python to query Oracle database
Use C++ to write Python3 extensions
How to write python configuration file
Python novice learns to raise usage
Use python to achieve stepwise regression
How to save the python program
Python is a cross-platform language code
Python ATM function implementation code example
Python implements 126 mailboxes to send mail
Using Python to implement multiple clipboards
How to install Python 3.8 on Ubuntu 18.04
How to write classes in python
How to filter numbers in python
Centos6.7 comes with python upgrade to
How to read Excel in Python