gpt4 book ai didi

python - 如何解决该打印语句的对齐方式?

转载 作者:行者123 更新时间:2023-12-03 08:21:57 24 4
gpt4 key购买 nike

我正在尝试创建一个表,该表遍历各种风速值,以便计算出如下所示的表:Ideal Run。但是,一旦我运行以下代码:

import math
y=' '
print("Big Blue Wind Chill")
print(y)
airTemp=float(input("Enter air temperature (F): "))
startWS=eval(input("Enter starting wind speed (mph): "))
print(y)
print("Temperature = " + str(airTemp) + " degrees F")
print(y)
entries = x = [[] for i in range(91-startWS)]
def Main():
i=startWS
for i in range(startWS,91):
entries[i-startWS].append(i)
i=i+1
Main()
def oldForumula():
i=startWS
for i in range(startWS,91):
old=round(0.081*(3.71*math.sqrt(i)+5.81-0.25*i)*(airTemp-91.4)+91.4)
entries[i-startWS].append(old)
i=i+1
oldForumula()
def newForumula():
i=startWS
for i in range(startWS,91):
new=round(35.74+0.6215*airTemp-35.75*(i**0.16)+0.4275*airTemp*(i**0.16))
#print(new)
entries[i-startWS].append(new)
i=i+1
newForumula()
def difference():
i=startWS
for i in range(startWS,91):
diff=round((0.081*(3.71*math.sqrt(i)+5.81-0.25*i)*(airTemp-91.4)+91.4)-(35.74+0.6215*airTemp-35.75*(i**0.16)+0.4275*airTemp*(i**0.16)),1)
#print(diff)
entries[i-startWS].append(diff)
i=i+1
difference()

print('Wind Speed', 'Old Formula', 'New Formula', 'Difference', sep='\t')
print(y)
for i in range(len(entries)):
for j in range(4):
print(entries[i][j], end='\t\t')
print()

我最终创建了表,但是列名和实际数值的对齐方式完全不对。运行以下代码后,结果如下所示: Actual run。如果您能帮助我修复对齐方式,我将不胜感激。

最佳答案

您可以使用Format Specification Mini-Language使用字符串格式。

例如:

for i in range(len(entries)):
print("{0:>6.2f}\t{1:>6.2f}\t{2:>6.2f}\t{3:>6.2f}".format(*entries[i]))

您得到的是这样的:
Wind Speed  Old Formula New Formula Difference

5.00 9.00 4.00 5.10
6.00 5.00 2.00 2.80
7.00 2.00 1.00 0.80
8.00 -1.00 0.00 -1.10
9.00 -3.00 0.00 -2.80
10.00 -5.00 -1.00 -4.30
11.00 -7.00 -2.00 -5.70

或者,使用更多的pythonic循环:
for entry in entries:
print("{0:>6.2f}\t{1:>6.2f}\t{2:>6.2f}\t{3:>6.2f}".format(*entry))

关于python - 如何解决该打印语句的对齐方式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54506548/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com