【发布时间】:2021-07-28 13:34:55
【问题描述】:
PYTHON 3.7.7
对于学校作业,我必须使用名为 Mimir 的邪恶程序提交代码。基本上,如果我的输出不是像素完美的,我会在测试用例上失败(15 分钟编写代码,50 分钟使输出“看起来正确”)。
问题:
我不知道如何让我的数字输出看起来像作业的。我在这里展示的尝试使用了“g”格式,因为它是唯一可以删除尾随零并在必要时删除小数点的格式修饰符。我需要能够在保持“.6”精度的同时做到这一点。另外,如何使输出永远不会以科学计数法显示。
感谢您的帮助!
我的密码:
#Asking user to define parameters for the calculation
Organs = float(input('Starting number of organisms: \n'))
DailyInc = float(input('Average daily increase: \n'))
DaysX = int(input('Number of days to multiply: \n'))
#Doing % to decimal calc. only one to make program work faster
Inc = (1 + (DailyInc / 100))
#Print table heading and first day
print('Day Approximate Population')
print('1', ' ', format(Organs, 'g'))
#Loop to calculate running daily total and print using format hell
for Day in range (2, DaysX + 1):
Organs = Organs * Inc
if Day >= 100:
print(Day," ", format(Organs, 'g'))
elif Day >= 10:
print(Day," ", format(Organs, 'g'))
else:
print(Day," ", format(Organs, 'g'))
我的输出(输入 - 2、30、60):
Starting number of organisms:
Average daily increase:
Number of days to multiply:
Day Approximate Population
1 2
2 2.6
3 3.38
4 4.394
5 5.7122
6 7.42586
7 9.65362
8 12.5497
9 16.3146
10 21.209
11 27.5717
12 35.8432
13 46.5962
14 60.575
15 78.7475
16 102.372
17 133.083
18 173.008
19 224.911
20 292.384
21 380.099
22 494.129
预期输出(输入 - 2、30、60):
Starting number of organisms:
Average daily increase:
Number of days to multiply:
Day Approximate Population
1 2
2 2.6
3 3.38
4 4.394
5 5.7122
6 7.42586
7 9.653618
8 12.549703
9 16.314614
10 21.208999
11 27.571698
12 35.843208
13 46.59617
14 60.575021
15 78.747528
16 102.371786
17 133.083322
18 173.008318
19 224.910814
20 292.384058
21 380.099275
22 494.129058
【问题讨论】:
-
尝试
f'{val1:<17.15f},而不是使用空格来格式化您的数据。我会发布一个例子
标签: python python-3.x