Give hints to user if no response

Viewed 79

I am trying to create a program that asks user for input with three responses to a question. Right now the questions I have are almost paragraph long, but ideally I would like to start by asking an initial question, then start giving hints if user doesn't input anything within any amount of time.

answers = []
def questions(question):
    print(question)
    for i in range(1, 4, 1):
        answers.append(input(f"{i}. "))

questions("""What are your three favorite things? """)

Ideally there would be something that behaved like pseudocode below:

ask user for input
    if no response within 30 seconds:
        give first hint
    elif no respose within 30 seconds:
        give second hint        

Thanks in advance!

1 Answers

You can create a hint process in which you wait for a while then output your hint. If user answers the question, terminate hint process with terminate().

import time
from multiprocessing import Process

answers = []

def questions(question):
    print(question)

    for i in range(1, 4, 1):
        answers.append(answer(f"{i}. "))

    print(answers)


def hint():
    # For testing simplification, I decrease the wait time

    time.sleep(5)
    print('\nhint1 after 5 seconds')

    time.sleep(3)
    print('hint2 after 3 seconds')


def answer(i):
    phint = Process(target=hint)
    phint.start()

    uanswer = input(i)
    phint.terminate()

    return uanswer

questions("""What are your three favorite things? """)

The output looks like

What are your three favorite things? 
1. 
hint1 after 5 seconds
hint2 after 3 seconds
test
2. 
hint1 after 5 seconds
test1
3. test3
['test', 'test1', 'test3']
Related