是否可以像对话一样在python中打印文本?
我有一个文本文件的故事与多个字符之间的对话。
其他文件中的文本:
Guru举起两个手指。
古鲁:两个男人从烟囱里下来。一张干净的脸出来,一张脏的脸出来。哪个人洗他的脸。
那个年轻人盯着大师看。
年轻人:这真的是逻辑上的考验吗?
古鲁人点点头。
年轻人:那张脏脸的人洗他的脸--他自信地回答。
古鲁:错了。那个脸干净的人洗他的脸。检查逻辑。那张脏脸的人看着那张干净的脸,认为他的脸是干净的。那个脸干净的人看着那张脏脸的人,认为他的脸很脏。所以,那张干净的脸会洗他的脸。
年轻人:非常聪明。再给我一次测试。
大师再次举起两根手指。
古鲁:两个男人从烟囱里下来。一张干净的脸出来,一张脏的脸出来。哪个人洗他的脸。
年轻人:我们已经确定了这一点。那个脸干净的人洗他的脸。
我的代码:
import time
print("This is the story i am telling.")
def storytelling():
with open('story.txt', 'r+') as f:
for line in f.readlines():
print(line, time.sleep(0.15))
storytelling()结果:
This is the story I am telling.
Guru holds up two fingers.
None
Guru: Two men come down a chimney. One comes out with a clean face, the other comes out with a dirty face. Which one washes his face.
None
The young man stares at the Guru.
None
Young Man: Is that really a test in logic.
None
The Guru nods.
None--我将尝试在以后的结果中找出如何摆脱任何一个
上面的文字可以是段式的,也可以是人对人的对话。使用def conversation():函数,因为它下一次使用。印刷必须像A说,他的故事线或段,有时间延迟,这样用户可以读取线,然后B说他的故事线和一个时间延迟,供用户阅读,等等。
我试着使用.readlines(),但是它没有用。这是逐行阅读,但我想一个人一个人地阅读。我可以在文本到文本之间的时间延迟上进行训练,但是怎样才能以人的方式打破文本之间的关系。
发布于 2021-01-21 13:03:04
要摆脱None,您必须删除print中的time.sleep(0.15)
一定是这样的:
from time import sleep
print("This is the story i am telling.")
def storytelling():
with open('story.txt', 'r+') as f:
for line in f.readlines():
print(line)
sleep(0.15)
storytelling()但在我的情况下,你不需要f.readline()。它还将与下列代码一起工作:
for line in f:
print(line)
sleep(0.15)如果你想一个人一个人的read,我能想到的只有两个解决方案与我的想象力。第一种方法是创建一个具有如下对话的.txt文件:
R
Guru holds up two fingers.
G
Guru: Two men come down a chimney. One comes out with a clean face, the other comes out with a dirty face. Which one washes his face. The young man stares at the Guru.
Y
Young Man: Is that really a test in logic.
R
The Guru nods.
Y
Young Man: The one with the dirty face washes his face
R
- he answers confidently.
G
Guru: Wrong. The one with the clean face washes his face. Examine the logic. The one with the dirty face looks at the one with the clean face and thinks his face is clean. The one with the clean face looks at the one with the dirty face and thinks his face is dirty. So, the one with the clean face washes his face.
Y
Young man: Very clever,. Give me another test.
R
The Guru again holds up two fingers.
G
Guru: Two men come down a chimney. One comes out with a clean face, the other comes out with a dirty face. Which one washes his face.
Y
Young man: We have already established that. The one with the clean face washes his face.然后用f.readline()来读对话,看看如果f.readline() == 'R',那么print。你说对了。
第二种解决方案是使用来自每个人的对话创建4-5个.txt文件,并结合代码来选择它们之间的对话。
如果你有更多的问题,请随便问。输入1注释@bilakos和我将得到一个通知
https://stackoverflow.com/questions/65827027
复制相似问题