我正在尝试创建一个表,该表遍历各种风速值,以便计算出如下所示的表: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/

10-11 01:34