How to pause and resume program execution in Python?

In Python, you can use the sleep() function from the time module to pause the program for a period of time. Here is an example of how to use it in your code:

import time

print("这是第一行代码")
time.sleep(2)  # 暂停执行2秒钟
print("这是第二行代码")

In the above code, the program will first print “This is the first line of code,” then execute time.sleep(2), pausing for 2 seconds, and finally print “This is the second line of code.”

To allow the program to continue running, you can use the input() function to wait for user input. The code example is as follows:

print("这是第一行代码")
input("按下Enter键继续...")
print("这是第二行代码")

In the code above, the program will first print “This is the first line of code,” then execute input(“Press Enter to continue…”), wait for the user to press Enter, and finally print “This is the second line of code.”

bannerAds