A
A
Amigun2019-02-04 17:36:35
Python
Amigun, 2019-02-04 17:36:35

How to make a specific response time in Python?

My code asks the question: The user must answer 1-yes, 2-no
print ('Газон зеленый')

print ('1 - Правда')
print ('2 - Ложь')

But the response time should be limited, for example 10 seconds. Using the time module
My code:
print ('Газон зеленый')
print ('1 - Правда')
print ('2 - Ложь')
ans = int(input('Ваш вариант ответа:'))
time.sleep (10)
print ('Время вышло!')
if ans == 1:
  print ('Вы ответили правильно!')
elif ans == 2:
  print ('Вы ответили не правильно!')
else:
  print ('Вы не ответили')

But the thing is, the time won't count down until someone types in an answer. How to fix? So that the time to answer the question was 10 seconds? (time.sleep (10))

Answer the question

In order to leave comments, you need to log in

3 answer(s)
B
BJlaDuMup, 2019-02-04
@Amigun

Works but needs to be improved

import time
from threading import Thread

an = 1
ans = 0

def countdown():
  time.sleep (10)
  if not ans:
    print ('\nВремя вышло!')
    global an
    an = 0
def answer():
  global ans
  ans = int(input('Ваш вариант ответа:'))
  if ans == 1 and an:
    print ('Вы ответили правильно!')
  elif ans == 2 and an:
    print ('Вы ответили не правильно!')
  else:
    ans = 3
    print ('Вы не ответили')


print ('Газон зеленый')
print ('1 - Правда')
print ('2 - Ложь')
task1 = Thread(target=countdown)
task2 = Thread(target=answer)
task1.start()
task2.start()

S
Soslan Khloev, 2019-02-04
@hloe0xff

Look here

A
Alexander, 2019-02-04
@NeiroNx

Well, how - I wrote a question, saved the time to a variable, waiting for an answer, the user answered - you compare the time with the saved one, if more than 10 seconds have passed, then you write that the time is up.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question