首页 > 解决方案 > 我正在学习 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)

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

标签: pythoninputprintingread-eval-print-loopsys

解决方案


你几乎拥有它!

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()

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


推荐阅读