1

Description: now, I have done input and countdown, both of which are carried out at the same time, but I want to achieve like this:

  1. When I don't input anything during the countdown, it will execute another function after the countdown
  2. When I input something before the end of the countdown, the countdown will pause, and then another function will be executed

My code is as follows:

import time
from threading import Thread


def waitinput():

    wait_input_str = input("Please enter your account:\n")
    print(wait_input_str)


thd = Thread(target=waitinput)
thd.daemon = True
thd.start()
for i in reversed(range(1, 11)):
    print("\rcountdown:{}second".format(i), end="")
    time.sleep(1)
# ###########################################

1 Answer 1

1

You can use isAlive() method to check if your thread has terminated.

In your case:

import time
from threading import Thread


def waitinput():

    wait_input_str = input("Please enter your account:\n")
    print(wait_input_str)


thd = Thread(target=waitinput)
thd.daemon = True
thd.start()
for i in reversed(range(1, 11)):
    if not thd.isAlive():
        # Execute 2
        print("\nCountdown has stopped")
        break
    print("\rcountdown:{}second".format(i), end="")
    time.sleep(1)

if thd.isAlive():
    # Execute 1
    print("\nCountdown has ended")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.