打印人与人之间类似文本的对话

2024-09-27 00:16:36 发布

您现在位置:Python中文网/ 问答频道 /正文

可以在类似python的对话中打印文本吗

我有一个文本文件,其中包含多个角色之间的对话故事

其他文件中的文本:

Guru holds up two fingers.

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.

Young Man: Is that really a test in logic.

The Guru nods.

Young Man: The one with the dirty face washes his face - he answers confidently.

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.

Young man: Very clever,. Give me another test.

The Guru again holds up two fingers.

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.

Young man: We have already established that. The one with the clean face washes his face.

我的代码:

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(),但没有用。这是一行一行地读,但我想一个人一个人地读。我可以计算文本之间的时间延迟,但如何在人与人之间断开文本


Tags: the文本cleannonewithoutoneface
1条回答
网友
1楼 · 发布于 2024-09-27 00:16:36

要除去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)

如果你想一个人接一个人,我只能用我的想象力想出两种解决办法。第一个是创建一个.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文件,并结合代码选择他们之间的对话

如果您有更多问题,请随时提问。键入一条注释@bilakos,我将收到通知

相关问题 更多 >

    热门问题