我正在做一个小程序,以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 :(得分:1)
这是因为您尝试使用"index"
格式化字符串":0"
,将其更改为"^10"
之类,并且至少会运行:
print(("{0:^10}{1:^9f}{2:^9f}").format("index", x, y))