Python ValueError:“=”字符串格式说明符中不允许对齐

Python ValueError:“=”字符串格式说明符中不允许对齐,python,string-formatting,Python,String Formatting,我正在做一个小程序来可视化一个混沌函数,其中有2个数字和迭代计数作为输入 在格式化打印时,我遇到了标题中所述的错误 我尝试使用{:^n}标记指定要占用的宽度,并使打印的值在该宽度内居中 def main(): print("This program illustrates a chaotic function") x = float(input("Enter a number between 0 and 1: ")) y = float(input("Enter ano

我正在做一个小程序来可视化一个混沌函数,其中有2个数字和迭代计数作为输入

在格式化打印时,我遇到了标题中所述的错误

我尝试使用{:^n}标记指定要占用的宽度,并使打印的值在该宽度内居中

def main():

    print("This program illustrates a chaotic function")
    x = float(input("Enter a number between 0 and 1: "))
    y = float(input("Enter another number between 0 and 1: "))
    n = int(input("How many iterations do you want to see?: "))

    print(("{0:0}{1:^9f}{2:^9f}").format("index", x, y))
    print("_" * 23)

    for i in range(n):
        x = 3.9 * x * (1 - x)
        y = 3.9 * y * (1 - y)
        print(("{0:^5}{1:^9}{2:^9}").format(range(n).index(i) + 1, x, y))


Error:

Traceback (most recent call last):
  File ".\chaos.py", line 18, in <module>
    main()
  File ".\chaos.py", line 10, in main
    print(("{0:0}{1:^9f}{2:^9f}").format("index", x, y))
ValueError: '=' alignment not allowed in string format specifier

输出应该是一个格式良好的表。

这是因为您正试图使用以下内容格式化字符串索引:0,更改为类似^10的内容,它至少会运行:


打印{0:^10}{1:^9f}{2:^9f}。formatindex,x,y

可能会很有帮助,但我用0:5的方式得到了它,我想知道为什么。0是否意味着占用所需的空间?你的意思是我应该用0^10?