我无法检测到'{'
或'}'
的字符。
此行:
if char == "{" or char == "}":
没有检测到'{'
或'}'
,我无法弄清楚。
not_one_space = False
text_example = "{} Welcome to...\n {}Minecraft{} but it's
text".format(text_color_green, text_color_blue, text_color_green)
for char in text_example:
sys.stdout.write(char)
sys.stdout.flush()
if char != " ":
if char == ".":
time.sleep(.4)
if char == "{" or char == "}":
()
else:
time.sleep(.1)
not_one_space = True
elif not_one_space == True:
time.sleep(.1)
not_one_space = False
实际结果:它没有检测到它,并且代码继续执行其他操作,这意味着它休眠了.1
秒,这是我不希望的。
预期:它将同时检测到'{'
和'}'
并运行if语句。
答案 0 :(得分:2)
在格式字符串替换的所有花括号上调用.format
。每一组都替换为format
的参数之一的字符串版本;如果它们不包含大括号,那么就没有剩了。
如果要插入这些值,但用大括号括起来,则每次使用时都需要添加两组额外的大括号,例如:
text_example = "{{{}}} Welcome to...\n {{{}}}Minecraft{{{}}} but it's text".format(text_color_green, text_color_blue, text_color_green)
答案 1 :(得分:1)
定义text_example
后,您将立即执行其format
方法。该方法除去括号,并将替换为该方法中作为参数提及的变量的值。
所以括号在代码中当然会消失了。
在text_example
中,括号的唯一方法是在format
方法中的参数值之一中包含括号。您不会显示这些参数值的值,但是给定它们的名称是不太可能的。