【问题标题】:Float Place Holder Prints Extra Zeroes浮动占位符打印额外的零
【发布时间】:2022-12-13 01:28:07
【问题描述】:

这是我的代码的摘录:

elif choice == "3":
    print("====================================\nPRINTING LIST...")
    #prints in this format: item name              item price      item quant and accesses the keys
    for i in glist:
        print("%s              %f              %d" %(i["item"], i["price"], i["quant"]))

基本上,该程序所做的是向用户询问商品、商品价格和数量,将其存储在字典中并以格式化格式打印。当用户选择 3 时,它应该执行上述代码块。

预期的结果应该是这样的: 用户输入: 项目:鸡蛋 价格:2 数量:3

预期成绩: 鸡蛋 2.00 3

我得到的结果: 用户输入: 项目:鸡蛋 价格:2 数量:3

预期成绩: 鸡蛋 2.0000000000 3

如何删除商品价格中多余的零?

我还没有尝试过任何东西,因为我不知道我应该做什么。

【问题讨论】:

标签: python dictionary


【解决方案1】:
for i in glist:
    # Use the format method to specify the number of decimal places to display
    print("{:<20} {:<10.2f} {:<10d}".format(i["item"], i["price"], i["quant"]))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-25
    • 2017-09-19
    相关资源
    最近更新 更多