价格= { “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/