我正在尝试以python但使用codio的方式为我的租车项目输出300.00的基本费用。我收到以下代码的结果是300.0,我缺少什么?

baseCharge = 0
if rentalCode == "B":[enter image description here][1]
    baseCharge = (rentalPeriod) * (budgetCharge)
elif rentalCode == "D":
    baseCharge = float(rentalPeriod) * float(dailyCharge)
elif rentalCode == "W":
    baseCharge = float(rentalPeriod) * float(weeklyCharge)
print(baseCharge)


上次运行时间为1/28/2019,9:37:55 AM
检查1失败
输出:
(B)预算,(D)每天还是(W)每周租金?
租用天数:
d
5
300.0
预期:
(B)预算,(D)每天还是(W)每周租金?
租用天数:
d
5
300.00

最佳答案

您需要输入:

print("{0:.2f}".format(baseCharge))

为了让Python以两位数字输出浮点数。

基本上将("{0:.2f}".format)添加到baseCharge print命令的前面。

09-30 09:28