Python program to read inputs within an interval of time

Python program to read inputs within an interval of time:

In this post, we will learn how to add a timer while reading inputs in Python. This program will ask the user to enter a value, it will wait for a specific time and if it don’t receive any input, it will exit.

These types of programs are useful if you are building apps that needs one layer of security. For example, you can exit the app if you don’t receive any input for a specific time interval.

With this program, you will learn how to use Timer objects of threading python module.

A quick introduction to threading module:

Using threads, we can execute more than one tasks at the same time. For example, you can use two threads and one can do some calculations and one can show graphical updates of the calculations parallely.

Threads has many advantages. Threads are normally used with time taking tasks. For example, if you are downloading a huge file, you can use a separate thread to do that. It will keep running in the background and it will not block any other parts of your application.

Python provides threading module to work with threads. Timer class is defined in threading module and using this class, we can run a specific action after a certain time interval.

Definition of Timer:

It is defined as like below:

Timer(t, func, args=None, kwargs=None)

It creates a Timer object that will run a function fun after a time interval t with arguments args and keyword arguments kwargs.

args and kwargs are optional. If we don’t provide these values, or if we provide None, it will use an empty list for args and an empty dictionary for kwargs.

The time should be in seconds.

It provides start() and cancel() methods. start() method is used to start the time. The cancel() method is used to cancel the timer if it is running.

Example of Timer:

Let me show you an example of Timer:

from threading import Timer

def say():
    print('Hello !!')

t = Timer(10, say)

t.start()

It will print the word Hello !! after 10 seconds.

Python program to read input with an interval of time:

We can use Timer class to write a program that will read user input within a time interval. Else, it will exit.

from threading import Timer
import sys


class Question:
    time_exceed = False

    def stop_program(self):
        self.time_exceed = True
        print('Time Up....Press enter to continue !!')

    def takeInput(self, msg):
        self.time_exceed = False
        print('\nYou have 5 seconds to enter...')
        t = Timer(5, self.stop_program)
        t.start()
        user_input = input(msg)
        t.cancel()
        return user_input


q = Question()

ans1 = q.takeInput('Enter a vowel: ')
if q.time_exceed == False:
    print(f'You have entered {ans1}')


ans2 = q.takeInput('Enter a number: ')
if q.time_exceed == False:
    print(f'You have entered {ans2}')

Here,

  • Question is a class that holds different methods and properties to read user inputs.
  • This class will read inputs if the input is entered within 5 seconds.

    • This class has a property time_exceed that is False by default. If the time for current question is exceeded 5 seconds limit, it will mark time_exceed as True.
  • We are creating one object of Question class and calling takeInput method with different questions to take inputs from the user.
  • This method returns the user input value.
  • If the time exceeds 5 seconds, it calls stop_program method that marks time_exceed as True.
  • We are checking this value and if it is False, we are printing the user entered value.

If you run this program, it will print output as like below:

You have 5 seconds to enter...
Enter a vowel: u
You have entered u

You have 5 seconds to enter...
Enter a number: 2
You have entered 2

You have 5 seconds to enter...
Enter a vowel: Time Up....Press enter to continue !!


You have 5 seconds to enter...
Enter a number: Time Up....Press enter to continue !!

You might also like: