Python Wait Time & User Input Handling – Essential Guide

Sometimes we want our python program to wait for a specific time before executing the next steps. We can use time module sleep() function to pause our program for specified seconds.

Python Wait Time

Let’s see a quick example where we will pause our program for 5 seconds before executing further statements.

import time

print('Hello There, next message will be printed after 5 seconds.')

time.sleep(5)

print('Sleep time is over.')

When we run this program, there will be 5 seconds delay between first print statement and second print statement.

User Input Handling

Sometimes we want to get some User Input through the console. We can use input() function to achieve this. In this case, the program will wait indefinitely for the user input. Once the user provides the input data and presses the enter key, the program will start executing the next statements.

sec = input('Let us wait for user input. Let me know how many seconds to sleep now.\n')

print('Going to sleep for', sec, 'seconds.')

time.sleep(int(sec))

print('Enough of sleeping, I Quit!')

 

Python Wait Time & User Input Handling – Essential Guide

Source: digitalocean.com

Create a Free Account

Register now and get access to our Cloud Services.

Posts you might be interested in:

Moderne Hosting Services mit Cloud Server, Managed Server und skalierbarem Cloud Hosting für professionelle IT-Infrastrukturen

Real-Time Water Billing with PHP, Redis Pub/Sub & MySQL

MySQL, Tutorial

Linux file permissions with this comprehensive guide. Understand how to utilize chmod and chown commands to assign appropriate access rights, and gain insights into special permission bits like SUID, SGID, and the sticky bit to enhance your system’s security framework.

Moderne Hosting Services mit Cloud Server, Managed Server und skalierbarem Cloud Hosting für professionelle IT-Infrastrukturen

Create a PHP REST API with JSON on Ubuntu 20.04

Tutorial, Ubuntu

Linux file permissions with this comprehensive guide. Understand how to utilize chmod and chown commands to assign appropriate access rights, and gain insights into special permission bits like SUID, SGID, and the sticky bit to enhance your system’s security framework.