繁体   English   中英

.format()的语法错误(Python 3.2.3)

[英]syntax error with .format() (Python 3.2.3)

因此,我试图编写一个程序,该程序编写一个单词,用markup(<或>)填充20个空格,然后向后写相同的单词。 它还需要用户输入。 谁能告诉我我做错了什么

 while test2 == 1 : 
    test = input("Enter a word to format. Entering QUIT will exit the program:")
    if test == ("quit"):
        print("You have quit the program.")
        break
    else:
        print("{0:*<20}{1:*>20})".format(
            "test")(["test"::-1])

语法上,您在最后一个print语句中的错误位置有一个"

print("{0:*<20}{1:*>20}").format("test")(["test"::-1])
                       ^

另外,您的format()参数还存在一些其他语法错误,这将适用于文字字符串“ test”,但您需要将其替换为变量,以将其用于实际用户输入:

print("{0:*<20}{1:*>20}").format("test","test"[::-1])

您的最后一行有错误,请尝试在单词之间添加20个空格

In [1]: s = "test"
In [2]: print('{0:<20}{1}'.format(s,s[::-1]))

test                tset

您还可以使用主观更清洁的print('{0}{1}{2}'.format(s," "*20,s[::-1]))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM