inputimeout 패키지
📌 inputimeout 패키지
: 설정한 시간 안에 키 입력을 받았는 지 확인하는 패키지.
inputimeout 패키지 설치
📌 inputimeout 패키지 설치
cmd창에 다음을 입력해 설치.
pip install inputimeout
inputimeout 패키지 사용
📌 inputimeout 사용
from inputimeout import inputimeout, TimeoutOccurred
try:
something = inputimeout(prompt='>>', timeout=5) # prompt : 출력할 내용, timeout : 제한 시간 설정.
except TimeoutOccurred: # 시간 안에 입력하지 않았을 경우, 아래 코드 실행.
something = 'something'
print(something)
💡 try except문
https://codingrecord2209.tistory.com/125?category=1105813
[Python] 예외처리 - try except문, try finally, try else문
예외처리 - try, except문 📌 try, except문 : try 블록 수행 중 오류가 발생하면 except문 실행. 📌 사용 방법 ① try, excpet만 사용. : 오류 종류와 상관없이 try 블록 수행 중 오류 발생 시, except문 실행. t..
codingrecord2209.tistory.com
inputimeout 패키지 사용 예시
❓ 랜덤으로 구구단 문제를 출력 후 답을 맞추는 프로그램.
(5초 동안 입력하지 않은 경우, 다음 문제로 넘어감.)
from inputimeout import inputimeout, TimeoutOccurred
import random
while True:
x, y = random.randint(2, 9), random.randint(1, 9)
print(f"{x} x {y} = ???")
try:
answer = inputimeout(prompt='>>>', timeout=5) # 제한 시간 5초로 설정.
if int(answer) == x * y: # 입력했을 때, 답이 맞으면 아래 코드 실행.
print("정답입니다.")
else: # 입력했을 때, 답이 틀리면 아래 코드 실행.
print("틀렸습니다.")
except TimeoutOccurred: # 시간 안에 입력하지 않았을 경우, 아래 코드 실행
print("입력 시간 초과")
print()
+ 추가 설명
더보기
5초 동안 입력하지 않았을 경우, "입력 시간 초과" 출력.
입력한 값이 정답일 경우, "정답입니다." 출력.
입력한 값이 오답일 경우, "틀렸습니다." 출력.
'Python > 라이브러리' 카테고리의 다른 글
[Python/라이브러리] gTTS 라이브러리 (0) | 2022.10.12 |
---|---|
[Python/라이브러리] googletrans 라이브러리 (0) | 2022.10.12 |
[Python/라이브러리] random 모듈 (0) | 2022.10.11 |
[Python/라이브러리] time 모듈 (0) | 2022.10.11 |
[Python/라이브러리] os 모듈 (0) | 2022.10.11 |
댓글