본문 바로가기
Python/Python_basic

타임 딜레이

by Mr.DonyStark 2023. 11. 8.

□ 일정 시간을 지정하여, 지정된 시간만큼 간격으로 프로그램이 실행되도록 할 때 사용

타임 라이브러리 호출

time.spleep(간격시간지정)

#1부터 10까지 2초 간격으로 숫자를 출력 후 종료
import time

#반복문 : for문
for i in range(1,11):
    time.sleep(2)  #2초마다 출력
    print(i)
    

#반복문 : while문
n=0
while n<10:
    n+=1 #증감연산자
    time.sleep(2)
    print(n)
    
s=0
while True:
    s+=1
    time.sleep(2)
    print(s)
    if s == 10:
        break
        
#0.5부터 3까지 0.5초 간격으로 딜레이를 증가 하면서 아래와 같이 출력

import time

#방법1
for i in [0.5,1, 1.5, 2, 2.5, 3]:
    time.sleep(i)
    print(f"Delayed for {i} Seconds")
    
#방법2
n=0.5
while True:
    time.sleep(n)
    print(f"Delayed for {n} Seconds")
    n+=0.5
    if n>=3.5:
        break
        
#방법3
def sleep_out(n=1):        #함수정의
    print(f"Delayed for {n} Seconds")
    time.sleep(n)

for n in [0.5,1, 1.5, 2, 2.5, 3, 3.5]:
    sleep_out(n)             #함수호출