create a loop of hello world for 10s in python

main.py
import time

start_time = time.time()
seconds_to_run = 10

i = 0
while time.time() - start_time < seconds_to_run:
    i += 1
    print(f"Hello World {i}")
155 chars
10 lines

This code block uses a while loop and the time module to print out "Hello World" repeatedly for 10 seconds. The start_time variable keeps track of when the loop started running, and the seconds_to_run variable specifies how long the loop should run for. The i variable is a counter that increments with each iteration of the loop, so that the output displays the number of times "Hello World" has been printed.

related categories

gistlibby LogSnag