Four common Bug types

Posted by smclay on Sat, 19 Feb 2022 08:38:33 +0100

1. Carelessness

pwd = input('Please input a password:')
if pwd == '520666'
     print('correct')

This is the code to verify the user password. If you put it into operation, the following error will be reported.
Find the error in this code and modify the code to make it run successfully:

pwd = input('Please input a password:')
if pwd == '520666':
    print('correct')

If you look closely, you will find that this code is missing a colon.

Carefully observe the error message: we can get three effective Keywords:

1. line2 second line. 2. ^ after pwd = = '520666', which is the end of the second line. 3. Syntax error means syntax error, that is, the syntax is not standardized.

Through such error information, we can quickly locate the bug and solve it according to the error information.

Please find the error in this code and modify the code to make it run successfully:

Tip: there are many errors in this code. Run it first and solve them one by one according to the error information.

for i in range(1,10): 
    print(i)
for i in range(1,10):
     print(i)




2. Unskilled knowledge
Next, let's look at the bug s caused by unskilled knowledge.

list = ['Liu Bei','Guan Yu','Fei Zhang','Zhao Yun']
print(list[4])
list = ['Liu Bei','Guan Yu','Fei Zhang','Zhao Yun']
print(list[3])

The mistake is obvious
The index of the list index out of range is out of range.

The list subscript starts from 0. 0, 1, 2, 3 list[4] is the fifth element of the list, but the list has only four elements.

Let's take another look: a student needs to delete an element in the list, but the result is unsuccessful.

list = ['Liu Bei','Guan Yu','Fei Zhang','Zhao Yun']
list.del(list[3])
print(list)
list = ['Liu Bei','Guan Yu','Fei Zhang','Zhao Yun']
del(list[1])
print(list)

The problem is in the second line. Look at the original notes. The del() function can directly put the deleted data in parentheses. There is no need to write "list." in front.
3. Unclear thinking
"Unclear thinking bug" is the most common bug for beginners. Solving it will solve most of the bugs.

The main reason for the unclear thinking bug is that when we face the problem, we don't think about the problem and details in place, which leads to "one move is careless and the overall error is reported". Sometimes we don't report errors, but we can't achieve the effect we want.

In view of this: two treasure boxes are provided for us in the Jianghu secret script.

First look at the print() function, which can print the content.

We can use the print() function to print out the results of each step, and then analyze the results to find the inconsistency between the computer running results and our analysis.

#We have also used notes. If a line or a piece of code is always written incorrectly, you can use comments to comment out the current code and run it step by step to eliminate errors.

The print() function works better with # annotations.

film = {
    'Fast & Furious':['Johnson','Stanson'],
    'Fire Hero':['Huang Xiaoming','Dujiang','Yang Zi'],
    'Late night canteen':['Liang Jiahui','Liu Tao'],
    'risk danger in desperation':['Dapeng','Li Meng','Ou Hao'],
    'Apostle Walker':['Zhang Jiahui','Gu Tianle']
}

star =input('Which actor do you want to see?')
for i in film:
    actors=[i]
    if star in actors:
        print(star+'Starring in film'+i)

Is this problem tangled? No error is reported, and the effect we want is not achieved.

Don't worry, solve it bit by bit.

film = {
    'Fast & Furious':['Johnson','Stanson'],
    'Fire Hero':['Huang Xiaoming','Dujiang','Yang Zi'],
    'Late night canteen':['Liang Jiahui','Liu Tao'],
    'risk danger in desperation':['Dapeng','Li Meng','Ou Hao'],
    'Apostle Walker':['Zhang Jiahui','Gu Tianle']
}
print(film)
# star =input('which actor's movie do you want to see? ')
# for i in film:
#     actors=[i]
#     if star in actors:
#         print(star + 'starring in film' + i)

I annotate the code behind the film, and then print the data content of the film, which can be printed normally. This shows that there is no problem with the dictionary film. Then let's look for the following code.

This time, let's take a look at the sentence star=input("which actor's movie do you want to see?"); We will print the content of star, and we will print the comments later.

film = {
    'Fast & Furious':['Johnson','Stanson'],
    'Fire Hero':['Huang Xiaoming','Dujiang','Yang Zi'],
    'Late night canteen':['Liang Jiahui','Liu Tao'],
    'risk danger in desperation':['Dapeng','Li Meng','Ou Hao'],
    'Apostle Walker':['Zhang Jiahui','Gu Tianle']
}

star =input('Which actor do you want to see?')
print(star)
# for i in film:
#     actors=[i]
#     if star in actors:
#         print(star + 'starring in film' + i)

Enter the actor's name and print normally. This shows that there is no problem with the star statement.

There is no problem with the above two parts. Let's check the content of the third part, the for loop. In fact, the old driver can see at a glance that there is a problem inside the for cycle.

Step by step.

for i in film: this line of code is written in a standard way without dropping the colon: then go on.

actors=[i] here is the assignment, how to assign I to the variable actors Let's print it and see what the output is?

film = {
    'Fast & Furious':['Johnson','Stanson'],
    'Fire Hero':['Huang Xiaoming','Dujiang','Yang Zi'],
    'Late night canteen':['Liang Jiahui','Liu Tao'],
    'risk danger in desperation':['Dapeng','Li Meng','Ou Hao'],
    'Apostle Walker':['Zhang Jiahui','Gu Tianle']
}

star =input('Which actor do you want to see?')
print(star)
for i in film:
    actors=[i]
    print(actors)
#     if star in actors:
#         print(star + 'starring in film' + i)


Why are they all movie names? The key of the dictionary is printed. I don't want dictionary keys, I want dictionary values.

How to modify? Look at the code modified by xiaok:

film = {
    'Fast & Furious':['Johnson','Stanson'],
    'Fire Hero':['Huang Xiaoming','Dujiang','Yang Zi'],
    'Late night canteen':['Liang Jiahui','Liu Tao'],
    'risk danger in desperation':['Dapeng','Li Meng','Ou Hao'],
    'Apostle Walker':['Zhang Jiahui','Gu Tianle']
}

star =input('Which actor do you want to see?')
print(star)
for i in film:
    actors=film[i]
    print(actors)
#     if star in actors:
#         print(star + 'starring in film' + i)

At this point, all actor names have been saved into the actors variable.

Then open the comment, open the two lines of if statement and start running.

film = {
    'Fast & Furious':['Johnson','Stanson'],
    'Fire Hero':['Huang Xiaoming','Dujiang','Yang Zi'],
    'Late night canteen':['Liang Jiahui','Liu Tao'],
    'risk danger in desperation':['Dapeng','Li Meng','Ou Hao'],
    'Apostle Walker':['Zhang Jiahui','Gu Tianle']
}

star =input('Which actor do you want to see?')
for i in film:
    actors=film[i]
    if star in actors:
        print(star+'Starring in film'+i)


4. Passive pit dropping
Passive pit dropping refers to that sometimes the code is OK, but the user's operation is incorrect, resulting in program problems.

For example: run the following code. You enter a string to see if the program reports an error? Run it again and enter an integer to try.

age = int(input('How old are you?'))
if age < 18:
    print('You can't smoke or drink. It's hot')

When inputting numbers, the program runs normally and will not report errors.

The program throws a bug when a non number is entered.


It throws us a bug, which is located in the first line and displays the bug information as "ValueError". ValueError refers to the data exception, that is, the data we enter is out of specification.

In the face of this problem, we seem to have no way to start?

Don't panic. In the Python Jianghu, Python provides us with an exception handling mechanism to internally digest the exceptions and let the program continue to execute.

In this way, we can modify the above code as follows, click Run and enter an integer to try; Try again by entering a string.

while True:
        try:
            age = int(input('How old are you?'))
            break
        except ValueError:
            print('What you entered is not a number')
if age < 18:
    print('You can't smoke or drink. It's hot')

Is it perfect this time?

This is the exception capture in Python.

This Code: 1. I don't know when the user will input correctly and when it will input incorrectly. Set the while loop to receive the input. As long as the user doesn't input a number, it will cycle all the time. After the user inputs a number, it will break out of the loop.

2. Using the try... except... Exception capture mechanism, users will always be prompted when their input is incorrect.

Well, we have introduced all four types of bug s and solutions.
For all the bugs, we should think more, summarize more notes and improve the ability of debug ging.
practice:
Running the following code will report an error; List is an empty list. We need to add 'X', 'Y' and 'Z' to the list and change the list into list = ['X', 'Y', 'Z']

list = []
list.append ('X','Y','Z')
print(list)

list = []
list.append ('X')
list.append ('Y')
list.append ('Z')
print(list)
num = [5,6,0,10]
for i in num:
    print (600/i)

Run the following code, the terminal will report an error, find the wrong place of the code and modify it

num = [5,6,0,10]
for i in num:
    if i!=0:
        print (600/i)

Topics: Python list