我在python的返回字符串后有一个空间,我不知道如何摆脱或从哪里来

发布于 2025-01-31 19:40:20 字数 1889 浏览 3 评论 0原文

我下面的代码只是一个有趣的小型Lib发电机,它可以帮助开始学习如何在Vscode上使用Python进行编程。我正在使用python3,我相信我不知道这是否有所作为,但是,当我打印字符串part1时,c_noun1和“ -footed”之间有一个空间,我不想要和不知道如何摆脱那个空间。我觉得有一种简单的方法可以做到。我正在考虑使用split()并以这种方式取出空间,但我认为这太多了。有人可以帮我摆脱将c_noun1和“脚式脚步”分开的空间。我真的希望该陈述可以打印“八英尺”而不是八英尺。

from operator import truediv

def colored(r, g, b, text):
    return "\033[38;2;{};{};{}m{} \033[38;2;255;255;255m".format(r, g, b, text)

def word(string):
    retry = True
    while (retry):
        print(string)
        a = input()
        if(a.isnumeric()):
            print("Please do not enter a number!")
            retry = True
        else:
            retry = False
    return a

opening = "This is a Mad Lib Generator!"      
Title = colored(255, 0, 0, opening)

print(Title)

#Stores users inputs
noun1 = word("Please enter a noun")
pnoun1 = word("Please enter a noun(plural)")
noun2 = word("Please enter a noun")
pnoun2 = word("Please enter a noun(plural)")
noun3 = word("Please enter a place")
adj = word("Please enter a adjective")
noun4 = word("Please enter a noun")

#colouring words
c_noun1 = colored(0, 255, 0, noun1)
c_pnoun1 = colored(0, 255, 0, pnoun1)
c_noun2 = colored(0, 255, 0, noun2)
c_pnoun2 = colored(0, 255, 0, pnoun2)
c_noun3 = colored(0, 255, 0, noun3)
c_adj = colored(0, 255, 0, adj)
c_noun4 = colored(0, 255, 0, noun4)

# #sentences with the string variables
part1 = "Be kind to your " + c_noun1 + "-footed" + c_pnoun1
part2 = "For a duck my be somebody's " + c_noun2 + ","
part3 = "Be kind to your " + c_pnoun2 + "in " + c_noun3
part4 = "Where the weather is always " + c_adj
part5 = "You may think that this is the " + c_noun4
part6 = "Well it is."

#print sentences
print(part1.lower())
print(part2.lower())
print(part3.lower())
print(part4.lower())
print(part5.lower())
print(part6.lower())

My code below is just a fun, small Mad Lib Generator to help start learning how to program in python on vscode. I am using python3 I believe I don't know if that makes a difference, however, when I print the string part1 there is a space between c_noun1 and "-footed" that I don't want and don't know how to get rid of that space. I feel like there is a simple way to do it. I'm thinking of using split() and taking out the space that way, but I think that's too much. Can someone please help me get rid of the space separating the c_noun1 and "-footed". I really want the statement to print "eight-footed" instead of eight -footed.

from operator import truediv

def colored(r, g, b, text):
    return "\033[38;2;{};{};{}m{} \033[38;2;255;255;255m".format(r, g, b, text)

def word(string):
    retry = True
    while (retry):
        print(string)
        a = input()
        if(a.isnumeric()):
            print("Please do not enter a number!")
            retry = True
        else:
            retry = False
    return a

opening = "This is a Mad Lib Generator!"      
Title = colored(255, 0, 0, opening)

print(Title)

#Stores users inputs
noun1 = word("Please enter a noun")
pnoun1 = word("Please enter a noun(plural)")
noun2 = word("Please enter a noun")
pnoun2 = word("Please enter a noun(plural)")
noun3 = word("Please enter a place")
adj = word("Please enter a adjective")
noun4 = word("Please enter a noun")

#colouring words
c_noun1 = colored(0, 255, 0, noun1)
c_pnoun1 = colored(0, 255, 0, pnoun1)
c_noun2 = colored(0, 255, 0, noun2)
c_pnoun2 = colored(0, 255, 0, pnoun2)
c_noun3 = colored(0, 255, 0, noun3)
c_adj = colored(0, 255, 0, adj)
c_noun4 = colored(0, 255, 0, noun4)

# #sentences with the string variables
part1 = "Be kind to your " + c_noun1 + "-footed" + c_pnoun1
part2 = "For a duck my be somebody's " + c_noun2 + ","
part3 = "Be kind to your " + c_pnoun2 + "in " + c_noun3
part4 = "Where the weather is always " + c_adj
part5 = "You may think that this is the " + c_noun4
part6 = "Well it is."

#print sentences
print(part1.lower())
print(part2.lower())
print(part3.lower())
print(part4.lower())
print(part5.lower())
print(part6.lower())

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

软糯酥胸 2025-02-07 19:40:20

尝试删除}\ 033之间的空间,有色函数,例如:

def colored(r, g, b, text):
    return "\033[38;2;{};{};{}m{}\033[38;2;255;255;255m".format(r, g, b, text)

Try removing the space between the } and \033 in the colored function, like this:

def colored(r, g, b, text):
    return "\033[38;2;{};{};{}m{}\033[38;2;255;255;255m".format(r, g, b, text)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文