본문 바로가기
개념/혼자 공부하는 파이썬

python) Counting Loop Iterations in a 5-Second Interval.

by kiseno 2024. 11. 15.
728x90
반응형
SMALL

import time

number = 0
target_tick = time.time() + 5
while time.time() < target_tick:
    number += 1
print(" 5초 동안 {} 반복하였습니다".format(number))

1. **`import time`**: This line imports the `time` module, which provides various time-related functions. It's used here to work with time operations, such as getting the current time and performing time-based calculations.

2. **`number = 0`**: Initializes a variable named `number` to 0. This variable is used to count how many times a loop executes.

3. **`target_tick = time.time() + 5`**: Sets a variable `target_tick` to the current time plus 5 seconds. `time.time()` returns the current time in seconds since the Epoch (January 1, 1970, 00:00:00 UTC). This line essentially sets a future time that is 5 seconds from the current moment.

4. **`while time.time() < target_tick:`**: Begins a `while` loop that will continue to execute as long as the current time is less than `target_tick`. This condition effectively creates a loop that runs for 5 seconds.

5. **`number += 1`**: Inside the loop, it increments `number` by 1 each time the loop executes. This counts how many loop iterations occur within the 5-second duration.

6. **`print(" 5초 동안 {} 반복하였습니다".format(number))`**: After the loop ends (i.e., after 5 seconds have passed), it prints a message stating how many times the loop has iterated during that 5-second interval. The message is in Korean, which translates to "Repeated {} times in 5 seconds".

 

 

 

728x90
반응형
LIST