Monday, April 21, 2025

How to Clear Screen in Terminal - Python

import os, time

for x in range(5):
    os.system('cls')
    print('Do you feel lucky, punk ? ')
    time.sleep(2)

This Python code imports two modules, "os" and "time", using the "import" statement.

The code then enters a "for" loop that iterates over a range of values from 0 to 4 (inclusive). In each iteration of the loop, the code executes two statements:

  1. The "os.system()" function is used to execute a command on the operating system shell. In this case, the command being executed is 'cls', which is used to clear the console screen on Windows systems.

  2. The "print()" function is used to print a message to the console screen. In this case, the message being printed is "Do you feel lucky, punk ? ".

After printing the message, the "time.sleep()" function is used to pause the program execution for 2 seconds, which creates a delay before the next iteration of the loop.

The combination of these statements creates a program that clears the console screen and displays a message repeatedly for a total of 5 times, with a delay of 2 seconds between each message.

So, when the program is run, the console screen will be cleared and the message "Do you feel lucky, punk ?" will be printed to the screen repeatedly for a total of 5 times, with a delay of 2 seconds between each message.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...