Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to choose execution statement and Program debugging in python programming

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about how to choose execution statements and program debugging in python programming. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it.

1. Description of program requirements

Now, let's write a program like this: after the program runs, it first prompts the executor to enter a name, and then prompts the user for age. If the age is less than 7, output "Hello, XXX kid" (note: here XXX is the name just entered); if the age is less than or equal to 15 years old, output "Hello, XXX youth"; if the age is less than 18 years old, output "Hello, XXX youth"; if the age is less than 40 years old, output "Hello, XXX youth"; if the age is less than 60 years old, output "Hello, XXX middle-aged" If the age is greater than or equal to 60, then output "Hello, XXX elderly".

We analyze the above program requirements scenario: after entering the name and age, splice different words (children, teenagers, teenagers, young people, middle-aged, elderly, etc.) to "Hello, XXX" according to the age.

According to previous experience, you can use the input () function (this kind of statement with () is called a function call, here we can remember the term "function", as for when the function, we will explain in detail later) to get keyboard input, print () function when the relevant content on the screen output.

2. If statement

The if statement executes certain lines of code by using a logic to determine whether the condition is true. The form of the if statement:

If judgment condition: the statement executed when the condition is true is written here. The statement executed when the else condition is not true is written here.

The if keyword is followed by a judgment condition. If the judgment condition is true, the code with indented format immediately after if is executed, and if the condition is not true, code with indented format immediately after else is executed. If no statement is executed when the condition is not valid, the code in this else section can be omitted. The judgment condition here can be the comparison operation of whether it is equal in mathematics, whether it is greater than, whether it is less than, whether it is greater than or equal, whether it is less than or equal, and so on.

With a basic understanding of the role and syntax of [if], let's implement the above program requirements step by step.

3. Program realization

According to the description of the requirement scenario in chapter 1, we can use if statements to determine whether the condition is true or not, in order to choose to execute different statements that can output different contents to the screen. Create a new file "nameandage.py" (for more information on the process of creating a new file in VSCode, see "II. Important basic concepts related to programming"), and then enter the following in the file:

# prompt: wait for the name to be entered, then store it in a box called name (variable) name = input ("Please enter the name:") # prompt, wait for the input age, and save it in the box called age (variable) age = input ("Please enter age:") # after converting the age of the string into the age of the number, save it to the variable age age = int (age) if age

< 7 : #年龄小于7,执行输出语句,注意需要缩进 print("你好,", name, "小朋友!") 我们可以点击组合键【Ctrl+F5】,执行程序,然后在编辑器下部的区域输入姓名【海绵宝宝】,年龄输入【3】,程序执行后,变量name中保存的值时字符串【海绵宝宝】,变量age的值时数字【3】。由于【age < 7】(变量age的值是3,3小于7,条件成立)的条件成立,会执行【print("你好,", name, "小朋友!")】语句,因此会看到程序输出【你好, 海绵宝宝 小朋友!】。我们可以再次运行程序,输入姓名保持不变,输入年龄时输入【10】,运行程序后,由于【age < 7】(age的值是10,10大于7,条件不成立)的条件不成立,因此不会执行【print("你好,", name, "小朋友!")】语句,而且在该行代码之后没有任何代码(这里没有else部分代码),程序执行【if】判断后就结束了,因此我们不会看到程序有任何输出。 当根据需求描述,当输入【10】的时候,实际应该输出【你好,XXX少年】才符合要求啊。那么应该怎么做呢?之前的程序判断了【age < 7】的情况,那么我们照葫芦画瓢,我们判断另外一种情况:【age < 15】的情况: #提示,等待输入姓名,输入后存入名叫name的盒子(变量)中name = input("请输入姓名:")#提示,等待输入年龄,输入后存入名叫age的盒子(变量)中age = input("请输入年龄:")#将字符串的年龄转换为数字的年龄后,保存到变量age中age = int(age)if age < 7 : #年龄小于7,执行输出语句 print("你好,", name, "小朋友!")if age < 15 : #年龄小于10,执行输出语句 print("你好,", name, "少年") 注意:【if ...... :】语句后面的代码需要缩进,我们在后续的学习中再来讲解这个代码缩进的意义。 再次运行程序,当我们输入年龄【10】,那么【age < 7】条件不成立,不会执行【print("你好,", name, "小朋友!")】(注意:#开头的行为注释行,注释行是不会被执行的),【age < 15】条件成立,因此会执行【print("你好,", name, "少年")】,最后我们看到程序的输出符合我们前面对程序的需求描述。 于是,我们根据需求描述,最终我们的程序代码如下: #提示,等待输入姓名,输入后存入名叫name的盒子(变量)中name = input("请输入姓名:")#提示,等待输入年龄,输入后存入名叫age的盒子(变量)中age = input("请输入年龄:")#将字符串的年龄转换为数字的年龄后,保存到变量age中age = int(age)if age < 7 : #年龄小于7,执行输出语句 print("你好,", name, "小朋友!")if age < 15 : #年龄小于10,执行输出语句 print("你好,", name, "少年")if age < 18 : #年龄小于18,执行输出语句 print("你好,", name, "青少年")if age < 40 : #年龄小于40,执行输出语句 print("你好,", name, "青年")if age < 60 : #年龄小于60,执行输出语句 print("你好,", name, "中年人")if age >

= 60: # if the age is 60 or older, execute the output statement print ("Hello," name, "elderly")

According to the previous description of the requirement scenario, we click the [Ctrl+F5] key combination many times and run the program many times, each time entering a different age to verify that our program is correct.

4. Debug program

The code is finished, we move the mouse to the VSCode program editor code [age = int (age)] in front of the line number, in the left side of the mouse will appear a small dark red dot, the mouse will continue to move to the left, click the dark red dot, then the dark red dot will become a highlighted red dot, indicating that we in the program corresponding code and added a breakpoint. The function of this breakpoint is that when we execute a program, if we reach a line with a breakpoint, the program will be paused at the breakpoint line, and then we can execute the program step by step, line by line, by observing the execution of the program. so that we can judge whether the program is executed as we expect it to be. Click [F5] (or click the main menu Run--- > Start Debugging menu item to start debugging), and then select the "Python file debugging open Python file" item in the "Select Debug configuration" dialog box, as shown in the following figure:

Then, after entering the name [SpongeBob] and age [25] in the program running window at the bottom of the editor, the VSCode editor pauses on the line where the breakpoint is located, as shown in the following figure:

The paused line is a line of code that is about to be executed but has not yet been executed, and the background of the paused line is different from that of other lines. Click the triangle and bug icon on the left to turn on or off the program variable monitor, as shown in the following figure:

We can see that when the program is paused, there are two variables, age and name, the value of the string [25] and the string [SpongeBob], which is exactly what we just entered. Click the [F10] key or click [in the debug toolbar at the top of the text editor]

] icon, the line of code [age = int (age)] is executed, and after execution, we can see that the value in the variable age becomes the number 25 (there is no pair of 'before and after the value). After that, the program will be paused at the line of code [if age < 7:] (this line of code is about to be executed but has not yet been executed), and the program will skip the line of code [print ("Hello," name, "Kids!") because the condition of that line is not valid. ], after clicking the [F10] button again, the program will be paused on the [if age < 15:] line. After clicking [F10] several times, the program will be paused at the code line [if age < 40:]. Because this condition holds, after clicking [F10], the program will pause at the code line [print ("Hello," name, "Youth"), click the [F10] key again, and the console window at the bottom of the editor will output [Hello. The string of SpongeBob Youth. After that, the program is paused at the line of code [if age < 60:]. After clicking [F10] several times, the program is finished.

By clicking but not executing the button, tracking the code execution process, we found a problem: once the [if age < 7:] condition is established, the subsequent conditions are certainly no longer valid, of course, there is no need to judge, if you also judge, in addition to a waste of time, take up computer resources, there is no sense, so we use another way to optimize the following procedure.

It is strongly recommended that students add breakpoints every time they run the program, and then click [step execution button] or [F10] to complete the tracking execution of the program, so as to speed up the understanding of the process of program execution and lay the foundation for follow-up learning.

5. If-elif statement

Since once the condition is established, the subsequent condition judgment does not need to be judged again, then we can use the if-elif statement to optimize it. The basic if-elif statement is as follows:

If condition 1: statement executed when condition 1 holds elif condition 2: statement executed when condition 2 holds elif condition 3: statement executed when condition 2 holds else statement executed when none of the above conditions holds

According to the above description of the execution rules of the if-elif statement, we can optimize the program as follows:

# prompt: wait for the name to be entered, then store it in a box called name (variable) name = input ("Please enter the name:") # prompt, wait for the input age, and deposit it in the box called age (variable) age = input ("Please enter age:") # after converting the age of the string into the age of a number, save it to the variable age age = int (age) if age < 7: # Age < 7 Execute the output statement print ("Hello," name, "Children!") Elif age < 15: # Age < 10, execute output statement print ("Hello," name, "Juvenile") elif age < 18: # Age < 18, execute output statement print ("Hello," name, "Youth") elif age < 40: # Age < 40, execute output statement print ("Hello," name, "Youth") elif age < 60: # Age < 60 Execute the output statement print ("Hello," name, "middle-aged") else: # 60 or older, execute the output statement print ("Hello," name, "elderly")

Once again, we add a breakpoint in front of the code line [age = int (age)], click the [F5] key to start the program debugging, enter [Qitian Great Sage] and [16], and then click the [F10] key to execute step by step. We found that after experiencing [age < 7], [age < 15] and [age < 18], the information of "XXX adolescent" was printed because the condition of [age < 18] was established, but the subsequent [age < 40] and other judgments were not carried out.

Through the above exercises, we have completed the if statement and tracked the process of running the program by stepping through the statement. Tracking the method of running the program is a very important means to eliminate problems in the whole programming process. When the program does not run in accordance with the process we designed, we can track the execution process of the program code.

It is strongly recommended that students add breakpoints every time they run the program, and then click [step execution button] or [F10] to complete the tracking execution of the program, so as to speed up the understanding of the process of program execution and lay the foundation for follow-up learning.

The above is how to choose to execute statements and program debugging in python programming. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report