Tutorial

Python wait time, wait for user input

Published on August 3, 2022
author

Pankaj

Python wait time, wait for user input

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.

Python wait for user input

Sometimes we want to get some inputs from the user 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!')

Below short screen capture shows the complete program execution. python wait time, python wait for user input Surprising, there is no easy way to wait for user input with a timeout or default value when empty user input is provided. I hope these useful features come in future Python releases.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Pankaj

author

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
February 1, 2021

Congrats…you are a good person and help me a lot. Thanks…

- abel

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    February 4, 2021

    Hey, Is there some way to ask user to enter line but within a time limit in python I mean for example there is only 10s to write a line by user and if he didn’t write within 10s the program says TIME IS OVER.

    - Dani

      Try DigitalOcean for free

      Click below to sign up and get $200 of credit to try our products over 60 days!

      Sign up

      Join the Tech Talk
      Success! Thank you! Please check your email for further details.

      Please complete your information!

      Become a contributor for community

      Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

      DigitalOcean Documentation

      Full documentation for every DigitalOcean product.

      Resources for startups and SMBs

      The Wave has everything you need to know about building a business, from raising funding to marketing your product.

      Get our newsletter

      Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

      New accounts only. By submitting your email you agree to our Privacy Policy

      The developer cloud

      Scale up as you grow — whether you're running one virtual machine or ten thousand.

      Get started for free

      Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

      *This promotional offer applies to new accounts only.