Minimalism is the soul of Python | Python code to find bugs (10)

**Python code to find bugs (10) **

**Code design requirements of the previous period: **

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:

Please read the code carefully to find out the bugs!

**Correct answer: ** There are 2 questions in total. (For the answer time, see how many answers are correct?)

(1) One is a real bug. Please pay attention to the parameters of the range() method. Not only is the length of the X matrix list expected to be the maximum number of cycles, but also the value range of the loop variable i, which will be the index of the X list. Therefore, its starting value should not be 1, but 0, because the index counts from 0. Accordingly, there is no need to add 1 to len(X). The same is true for the following nested loops.

(2) The other is a problem that requires code optimization. This cannot be completely regarded as a bug, because the program can work. However, we say that the code should be as concise as possible, including not writing repetitive code. In many cases, the way we think about solving problems will directly affect the complexity of our code. In the above code, we define an empty list R to store the return value after matrix operation. Therefore, in the first round of the following cycle, we define an empty list Z to store the result of the addition of a corresponding row of the X matrix and the Y matrix in the second round of the cycle. The cycle ends. This result, Can be added to the R list. It seems that there is no problem with the idea, but can it be more concise? Why not define a two-dimensional R matrix from the beginning? In this way, can't we directly update the calculation results of X matrix and Y matrix directly into R? That's right, it can definitely be like this. However, the difference between the two is not formal, but the habitual complexity of thinking. We are always accustomed to using a one-dimensional linear way to consider problems in order, not used to a multi-dimensional parallel thinking mode. This case happens to help us think about such problems. Think about it, what is the difference between X[i] and X[i][j]? Which is more efficient? Why Python always defines some relatively complex data structures is to make the logic of dealing with problems simple, after all, dealing with problems is the goal.

So, the correct code should look like this:

**Comment: This code finds bugs, the main purpose: **

(1) Learn the matrix and recognize the expression of multi-dimensional data.

(2) Consolidate the basic grammar, and master the application of multiple parameters of the range() method.

(3) Train skilled application ability of complex data structure.

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 of this issue: ** Exchange the values of two variables.

code show as below:

After several trials of relatively complex issues, this issue is simple. 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

Minimalism is the soul of Python | Python code to find bugs (10)
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)
How to find the area of a circle in python
What is the use of Python
What is the scope of python variables
200 lines of Python code to achieve snake
What is the id function of python
What is the prospect of python development
What is the function body of python
What is the function of adb in python
Python implements the source code of the snake game
500 lines of python code to achieve aircraft war
Python how to move the code collectively right
What is the advantage of python over corporate language
How to practice after the python file is written
How to understand the introduction of packages in Python
Python simulation to realize the distribution of playing cards
Consolidate the foundation of Python (4)
Consolidate the foundation of Python(7)
Consolidate the foundation of Python(6)
Consolidate the foundation of Python(5)
Find the kernel source code corresponding to the running Ubuntu version
Is python code case sensitive
Consolidate the foundation of Python (3)
The usage of wheel in python
How to wrap in python code
Python handles the 4 wheels of Chinese
How to save the python program
Python is a cross-platform language code
Python simulation of the landlord deal
The premise of Python string pooling
Secrets of the new features of Python 3.8
How to view the python module
The father of Python joins Microsoft
The operation of python access hdfs
The usage of tuples in python
Is python crawler easy to learn
Python handles operation code to execl
End the method of running python
Can Python implement the structure of the stack?
What is the use of python's pip
Learn the basics of python interactive mode
How to verify successful installation of python
What are the required parameters of python
Logistic regression at the bottom of python
The usage of Ajax in Python3 crawler
Python solves the Tower of Hanoi game
Python string to judge the password strength
Solve the conflict of multiple versions of python
Python implements the sum of fractional sequences
Two days of learning the basics of Python
The essence of Python language: Itertools library
The specific method of python instantiation object