缓慢打印(模仿打字)

问题描述 投票:0回答:1

我正在尝试制作一个基于文本的游戏,我希望文本缓慢打印出来以模拟打字。我也希望它成为一个输入。可以这样做吗?

我看到其他人使用这个代码(粗体是我添加的):

import sys,time

def sinput(str):
  for c in str + '\n':
    sys.stdout.write(c)
    sys.stdout.flush()
    time.sleep(4./90)

slowtest=sinput('Does it work? ')
**if slowtest == 'yes':
    print('Amazing!')**

它能够像我想要的那样慢慢地打印出代码,不幸的是我无法回答这个问题(“它有效吗?”)。

python python-3.x
1个回答
2
投票

您可以将

input()
分配给
slowtest

sinput()
函数没有返回任何内容。

一旦打印缓慢,您可以使用

input()
作为
slowtest
变量,然后检查 if 条件。

import sys,time

def sinput(str):
  for c in str + '\n':
    sys.stdout.write(c)
    sys.stdout.flush()
    time.sleep(4./90)

sinput('Does it work? ')
slowtest = input()
if slowtest == 'yes':
    print('Amazing!')
© www.soinside.com 2019 - 2024. All rights reserved.