python看到代码并尝试帮助我 friend

标签 python python-3.x

价格= { “1”:1000, “2”:2000 年, “3”:3000, “4”:4000, “5”:5000, }

def get_total_challan_cost(): 总价 = 0

while True:
    challan_type = input("Enter the Challan Type: ")

    if challan_type == "":
        break

    price = prices[challan_type]
    total_price += price

    print("The price for Challan is: {}".format(price))

print("")
print("Total price: {}".format(total_price))

定义主函数(): # 生成 machine_cost、diesel_cost 等的代码...

# machine_cost = ...?
# diesel_cost = ...?
challan_cost = get_total_challan_cost()

# print(machine_cost + diesel_cost + challan_cost + ...)

如果名称 ==“主要”: main()

到这里就OK了

现在我在新行中打印以下内容 打印(get_total_challan_cost) 它应该只给出总 challan 成本,而不需要再次输入

最佳答案

使用累加器变量表示到目前为止的总成本,使用 dict 表示价格。

尝试这样的事情:

prices = {
    '1': 1000,
    '2': 2000,
    '3': 3000,
    '4': 4000,
    '5': 5000,
}

def get_total_challan_cost():
    total_price = 0

    while True:
        challan_type = input("Enter the Challan Type: ")

        if challan_type == "":
            break

        price = prices[challan_type]
        total_price += price

        print("The price for Challan is: {}".format(price))

    print("")
    print("Total price: {}".format(total_price))

def main():
    # code that generates machine_cost, diesel_cost etc ...

    # machine_cost = ...?
    # diesel_cost = ...?
    challan_cost = get_total_challan_cost()

    # print(machine_cost + diesel_cost + challan_cost + ...)

if __name__ == "__main__":
    main()

关于python看到代码并尝试帮助我 friend ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49112152/

相关文章:

Python:YAML 函数字典:如何在不转换为字符串的情况下加载

python - 解析时出现意外的 EOF;尝试计算每行读数的平均值/最大/最小值

python - Python 模块是否需要 shebang 或编码指令?

python - 将 f 字符串语法转换为在旧版本的 python 中打印

python - 如何等待强制子例程在继续下一行之前显示小部件

python-3.x - 适用于Python 3和Linux的Matplotlib

python-3.x - python没有定义递归函数吗?

python - 你能在每次修改另一个字段时修改一个对象的字段吗?

python - 如何将 Moviepy 大小调整为全屏?

python-3.x - 使用 Flask 上传文件