在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!
""")
你的or条款没有做你认为它正在做的事情。第一个检查这三件事中的任何一件是否为True:
character != "!"
bool(".")
bool("?")
请注意,2和3始终为真。
如果声明短路评估。如果字符输入是.,它将检查条件1并发现它是假的。然后它将包括评估中的条件2 False or "."。因为"."总是如此,它会短路和返回".",其评估结果为真。自己尝试一下,键入False or "."解释器,你会发现它返回"."。
就个人而言,我会用这样的set实现来做到这一点:
if c not in {"!", ".", "?"}:
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。