Python code to find bugs(7)

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

Python code to find bugs (2)
Python code to find bugs(7)
Python code to find bugs(4)
Python code to find bugs (3)
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
How to write Pythonic code
Python drawing rose implementation code
How to find the area of a circle in python
How to learn python quickly
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| 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 omit parentheses in Python
How to install Python 3.8 on CentOS 8
Python example to calculate IV value
How to install Python 3.8 on Ubuntu 18.04