Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python看了代码,试着帮我的朋友_Python_Python 3.x - Fatal编程技术网

python看了代码,试着帮我的朋友

python看了代码,试着帮我的朋友,python,python-3.x,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_pr

价格={ '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))
def main(): #生成机器成本、柴油成本等的代码

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

# print(machine_cost + diesel_cost + challan_cost + ...)
如果name==“main”: main()

直到这里一切都好 现在我用新行打印下面的内容 打印(获取总成本)
它应该只给出总的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()

只有一个输入?其他输入的总和?能否请您具体说明您想要的总和以及预期的输出是什么?我假设OP表示
的每次迭代,而
循环程序将迭代,直到我们键入space输入Challan类型:1 Challan的价格是:1000输入Challan类型:4 Challan的价格是:4000输入Challan类型:5 Challan的价格是:5000非常感谢兄弟的帮助你能告诉我任何可以学习Python编码的网站吗good@Dhanvanth我不能特别推荐任何Python教程,但互联网上有很多教程可以帮助您学习基本知识。一般来说,编程的一个好原则是“不要重复自己”。如果您发现自己在编写一个程序时使用了大量包含几乎相同代码块的
If
-
else
语句,请尝试找出如何重构它。@Dhanvanth欢迎您。如果这个答案解决了你的问题,考虑接受它:-在上面的一切都是好的,但我想打印总价格在其他行它不工作(TooSoalPalk)@ dhanvth-即输出现在看起来是什么样子,你想让它看起来像什么?