首页 > 解决方案 > 在 Python 中的不同行中打印

问题描述

我试图在不同的行中打印以下代码,但它总是在同一行中打印。任何人都可以帮忙吗?

import time
import sys


def delay_print(s):
    for c in s:
        sys.stdout.write(c)
        sys.stdout.flush()
        time.sleep(0.05)


delay_print("Hi there hope everything is fine.")
time.sleep(2)


def delay_print(s):
    for c in s:
        sys.stdout.write(c)
        sys.stdout.flush()
        time.sleep(0.05)


delay_print("This is a mail.")

这是显示给我的输出-

Hi there hope everything is fine.This is a mail.

如何像这样打印它们-

Hi there hope everything is fine.
This is a mail.

标签: python

解决方案


只需print("\n")在 for 循环之后添加。

函数是在要多次完成特定任务时编写的,不需要声明相同的函数,只需调用它,你就可以开始了!


推荐阅读