【问题标题】:python leaves line after printing functionpython在打印功能后离开线
【发布时间】:2019-08-05 06:36:58
【问题描述】:

我正在构建一个 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)

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

“仍在工作”的那一行应该是绿色的,但实际上是蓝色的。

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

【问题讨论】:

  • print(color('none', 'red', 'blue') + 'works\?') 你能试试这个吗? print(color('none', 'red', 'green') + 'works still\?')
  • 是的,如果您将其作为答案提交,我会将其标记为正确

标签: python python-3.x colors terminal module


【解决方案1】:
print(color('none', 'red', 'blue') + 'works\?') 
print(color('none', 'red', 'green') + 'works still\?')

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-05
    • 2011-11-19
    • 1970-01-01
    • 1970-01-01
    • 2019-05-01
    • 1970-01-01
    相关资源
    最近更新 更多