【问题标题】:Shipping Charges calculator not producing the correct result运费计算器没有产生正确的结果
【发布时间】:2022-09-29 02:46:18
【问题描述】:

下面是一个正在运行的程序示例:

Welcome to the Fast Freight Shipping Company shipping rate program. 
This program is designed to take your package(s) weight in pounds and 
calculate how much it will cost to ship them based on the following rates.

    2 pounds or less = $1.10 per pound
    Over 2 pounds but not more than 6 pounds = $2.20 per pound
    Over 6 pounds but not more than 10 pounds = $3.70 per pound
    Over 10 pounds = $3.80 per pound
    
    Please enter your package weight in pounds: 1
    Your cost is: $ 1.10
    Would you like to ship another package <y/n>? y
    Please enter your package weight in pounds: 5
    Your cost is: $ 11.00
    Would you like to ship another package <y/n>? y
    Please enter your package weight in pounds: 52
    Your cost is: $ 197.60
    Would you like to ship another package <y/n>? y
    Please enter your package weight in pounds: 8
    Your cost is: $ 29.60
    Would you like to ship another package <y/n>? y
    Please enter your package weight in pounds: 3
    Your cost is: $ 6.60
    Would you like to ship another package <y/n>? t
    Invalid Input
    Would you like to ship another package <y/n>? y
    Please enter your package weight in pounds: 10
    Your cost is: $ 37.00
    Would you like to ship another package <y/n>? n
    Thank you for your purchase, your total charge is $ 282.90 
    Goodbye!

这是我到目前为止所拥有的:

charges = 0
answer = \"yes\"

while (answer == \"yes\"):
 packageWeight = eval (input (\"Enter weight of package: \"))
 answer = input (\"Do you have more items to input? <yes/no>? \")

 if (packageWeight <=2):
     charges = (packageWeight * 1.10)
     print (\"The cost is: $\",charges)
 elif (packageWeight >2) and (packageWeight<=6):
     charges= (packageWeight * 2.20)
     print (\"The cost is: $\", charges)
 elif (packageWeight >6)and (packageWeight<=10):
     charges = (packageWeight * 3.70)
     print (\"The cost is: $\", charges)
 else :
     charges = (packageWeight * 3.80)
     print (\"The cost is: $\", charges)

message = \"Thank you for your purchase, your total charge is $\"
sum= charges+charges
message += format(sum, \',.2f\')

print(message)
print(\"Goodbye!\")

我如何找到所有费用的总和?我怎样才能让第二个问题出现在成本之后?

所以它会显示:

    Enter weight of package: 3
    The cost is: 6.60
    Do you have more items to input? yes/no

    标签: python


    【解决方案1】:

    我不太明白how would I find the sum of all charges这个说法,但我解决了你的第二个问题。

    这是代码:

    def get_charges(current_weight):
        if (current_weight <= 2):
            return (current_weight * 1.10)
        elif (current_weight > 2) and (current_weight <= 6):
            return (current_weight * 2.20)
        elif (current_weight > 6) and (current_weight <= 10):
            return (current_weight * 3.70)
        else:
            return (current_weight * 3.80)
    
    
    charges = 0
    answer = "yes"
    
    while (answer == "yes"):
        # Eval is not recommended, use a float and round if needed.
        packageWeight = float(input("Enter weight of package: "))
        charges += get_charges(packageWeight)
        print("Current charges: ", charges)
        answer = input("Do you have more items to input? <yes/no>? ")
    
    
    print(f"Thank you for your purchase, your total charge is ${charges}")
    print("Goodbye!")
    

    出于安全原因不推荐eval(),所以我把它改成了float()。

    如果您有任何问题,请告诉我

    【讨论】:

    • 为了澄清,我需要一起找到每笔费用的总和。所以输出的一个例子是: 请输入您的包裹重量(磅):3 您的成本是:$ 6.60 您想运送另一个包裹 <y/n> 吗? y 请以磅为单位输入您的包裹重量:10 您的费用为:$ 37.00 您想运送另一个包裹<y/n>吗? n 感谢您的购买,您的总费用为 43.60 美元
    猜你喜欢
    • 2015-06-23
    • 1970-01-01
    • 2014-03-14
    • 2020-01-16
    • 2011-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-01
    相关资源
    最近更新 更多