我正在尝试创建一个程序,要求用户输入对问题的三个答案。现在我遇到的问题几乎是一段长的,但理想情况下,我想首先提出一个初始问题,然后如果用户在任何时间内没有输入任何内容,则开始给出提示。
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? """)
Run Code Online (Sandbox Code Playgroud)
理想情况下,会有像下面的伪代码一样的行为:
ask user for input
if no response within 30 seconds:
give first hint
elif no respose within 30 seconds:
give second hint
Run Code Online (Sandbox Code Playgroud)
提前致谢!
您可以创建一个hint等待一段时间然后输出提示的过程。如果用户回答了问题,则使用终止()hint终止进程。
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? """)
Run Code Online (Sandbox Code Playgroud)
输出看起来像
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']
Run Code Online (Sandbox Code Playgroud)