我正在学习 python,请有人告诉我如何修复它,以便我可以慢慢打印每个字母就像打字机一样,但也可以使用输入

问题描述

door1 = input('\n\nthere are 3 doors ahead of you,\n\nwhich one do you pick? \n\n1,2 or 3.')
for char in door1:
    sys.stdout.write(char)
    sys.stdout.flush()
    time.sleep(0.1)

我试图让代码慢慢打印问题,但是当我试图同时获取输入时,我不知道如何做到这一点。

解决方法

你差点就拥有了!

import time,sys
door1 = '\n\nthere are 3 doors ahead of you,\n\nwhich one do you pick? \n\n1,2 or 3.'
for char in door1:
    sys.stdout.write(char)
    sys.stdout.flush()
    time.sleep(0.1)
response = input()

之前,您以 typewrite-style 编写用户响应。你想把问题写成这样,所以我把door1改成了你的问题串。然后慢慢打印后,我把输入功能放在那里。