Novice on the road, how to debug the program?

Hello everyone, welcome to Crossin's programming classroom!

When writing code, bugs will inevitably appear. When many people are learning programming for the first time, when they finish writing the program and run it, they find that the result is different from what they expected, or the program terminates unexpectedly, and they have no idea for a while. They don’t know where to start and can only re-run the program repeatedly Just look forward to a sudden result.

Today I will share some debugging experience and solving skills when the code encounters problems, hoping to help you.

1. Read the error message

Consider the following example:

import random
a = 0
for i in range(5):
    b = random.choice(range(5))
    a += i / b
print(a)

In this program, i cycles from 0 to 4, and b is a random number from 0 to 4 in each cycle. Add the result of i / b to a, and finally output the result.

Running this program, sometimes the result will be output, and sometimes an error message will pop up:

Traceback (most recent call last):
  File "C:\Users\Crossin\Desktop\py\test.py", line 6, in <module>
    a += i / b
ZeroDivisionError: integer division or modulo by zero

Some students panicked when they saw an English prompt. In fact, it's not that complicated. Python's error prompts are still very standard.

It tells us that the error occurs at line 6 in the test.py file

a += i / b

on this sentence.

This error is "ZeroDivisionError", which is a division by zero error.

"integer division or modulo by zero", the integer is divided by 0 or modulo by 0 (remainder).

Because 0 cannot be used as a divisor, this error will be raised when b is randomized to 0.

Knowing the reason, you can solve this bug smoothly.

When writing code in the future, if you encounter an error, don't rush to change the code. Try reading the error message to see what it says.

2. Output debugging information

We teach the output function "print" at the very beginning of all lessons. It is the simplest means of debugging in programming. Sometimes, the cause of the program error cannot be judged only from the error message, or no error occurs, but the result of the program is wrong. In this case, it can help analyze the program by outputting some states in the process of the program.

Modify the previous program and add some output statements that have nothing to do with the program function:

import random
a = 0
for i in range(5):
    print('i:', i)
    b = random.choice(range(5))
    print('b:', b)
    a += i / b
    print('a:', a)
    print()
print(a)

The output after running (the result will be different each time):

i: 0
b: 3
a: 0.0




i: 1
b: 4
a: 0.25




i: 2
b: 0
Traceback (most recent call last):
  File "C:\Users\Crossin\Desktop\py\test.py", line 8, in <module>
    a += i / b
ZeroDivisionError: integer division or modulo by zero

When the value of b is 0, a division by zero error occurs. This time you can see more clearly the state of the program when it goes wrong.

In real development, the structure of the program may be very complex. By outputting debugging information, you can effectively narrow down the scope, locate where the error occurred, confirm the scene when the error occurred, and find out the cause of the error.

There are other debugging techniques, such as using breakpoint debugging, using IDE error prompts, etc., you can refer to the articles I wrote before:

5 minutes to develop, 2 hours to debug - where is your problem?

How to debug with breakpoints in Python

In addition, now that there is an artifact like ChatGPT, it can also be used to assist debugging. This method is specially introduced in Crossin's new book " Action on Code: Learning PYTHON Programming from Zero Basics (CHATGPT Edition) ".

ae77dd7ef935842b01f762b49babbaf5.jpeg

This book strives to be easy to understand, so that zero-based "novice" who has no programming experience at all can learn Python. The content starts from the most basic steps of environment construction, and gradually goes deep into common practical applications. While explaining the knowledge points, it is equipped with corresponding code examples, so that readers can learn and practice to deepen their understanding.

The book covers Python environment construction, basic grammar, common data types, practical modules, regular expressions, object-oriented programming, multi-task programming and other knowledge points. In addition, three practical projects of crawler, GUI and game are provided.

The book also innovatively uses ChatGPT as an aid to programming learning, leading readers to explore a new mode of learning programming in the AI ​​era.

Readers and friends can join the reader communication group after purchasing, and Crossin will open the accompanying reading mode for you, answering all your questions when reading this book.

Thank you for retweeting and liking ~

 

Guess you like

Origin blog.csdn.net/qq_40523737/article/details/130896268
Recommended