开发者社区> 问答> 正文

Python 3.6 sleep()在同一个字符串内的不同睡眠时间取决于字符

在python 3.6中,这是我的代码。我的问题是我得到了字符之间的延迟,但我没有在句子之间得到更长的延迟。

import time
import sys

def delay_print(s):

for c in s:
    if c != "!" or "." or "?":
        sys.stdout.write(c)
        # If I comment out this flush, I get each line to print
        # with the longer delay, but I don't get a char-by char 
        # delay
        # for the rest of the sentence.
        sys.stdout.flush()
        time.sleep(0.05)
    elif c == "!" or "." or "?":
        sys.stdout.write(c)
        sys.stdout.flush()
        time.sleep(3)

delay_print( """

Hello.
I want this to have an added delay after sentence-ending 
punctuation?
But I also want it to have a shorter delay after each character 
that isn't one of those chars.
This is supposed to mimic speech patterns. Like if you've ever 
played SNES Zelda: A Link to the Past.
Why isn't this code doing what I want it to?.
What I've written is broken and I don't know why!

""")

展开
收起
一码平川MACHEL 2019-02-28 14:21:27 2021 0
2 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载
http://www.vxiaotou.com