我在python的返回字符串后有一个空间,我不知道如何摆脱或从哪里来
我下面的代码只是一个有趣的小型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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试删除
}
和\ 033
之间的空间,有色
函数,例如:Try removing the space between the
}
and\033
in thecolored
function, like this: