gpt4 book ai didi

python 在打印函数后留下一行

转载 作者:行者123 更新时间:2023-12-01 07:28:02 25 4
gpt4 key购买 nike

我正在构建一个 python 模块,如下所示。您可以通过编写以下内容来使用它:

from color import *

print(color('none', 'red', 'blue') + 'works?')
print(color('none', 'red', 'green') + 'works still?')

该代码使用转义码,工作原理如下:

def color(style, fore, bg):

a = "\033[0;"
b = "39;"
c = "49m"

#styles
if style == "none":
a = "\033[0;"

#fore
if fore == "none":
b = "39;"

#background
if bg == "none":
c = "48m"

#finish
return(a + b + c)

一切正常,除了背景颜色有时会打印额外的一行,如下所示 screenshot

“仍然可以工作”的行应该是绿色的,但实际上是蓝色的。

有什么想法可以解决这个问题吗? (如果需要,我可以提供更多代码

最佳答案

print(color('none', 'red', 'blue') + 'works\?') 
print(color('none', 'red', 'green') + 'works still\?')

这似乎是一个转义字符问题,我很高兴它为您解决了。

关于python 在打印函数后留下一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57353314/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com