【问题标题】:why won't Postal Rate code work为什么邮政费率代码不起作用
【发布时间】:2016-02-01 22:24:14
【问题描述】:

我需要帮助我得到的错误是未定义成本。该程序根据运输类型和重量计算邮政费率。我不确定我做错了什么,一些建议将非常感谢

def main() :
    '''The program calculates the cost of postal rates through the type of
    shipping and the weight of the package'''

Shipping = int(input("Shipping: Enter (1) stamps or (2) for meter postage: "))
Size = int(input("Package size: Enter (1) for standard size or (2) for 
oversize: "))
Weight = int(input("What is the weight in grams of the envelope?: "))

#Standard shipping
if Shipping == "1" :
        if Size == "1" :
            if weight <= 30 :
                cost = "1.00"
            elif weight  >=31 <=50 :
                cost = "1.20"
if Shipping == "2" :
        if Size == "1" :
            if weight <= 30 :
                cost = "0.77"
            elif weight  >=31 <=50 :
                cost = "1.18"
#Oversize shipping
if Shipping == "1" :
        if Size == "2" :
            if weight <= 100 :
                cost = "1.80"
            elif weight  >101 <=200 :
                cost = "2.95"
            elif weight  >=201 <=300 :
                cost = "4.10"
            elif weight  >=301 <=400 :
                cost = "4.70"
            elif weight  >=401 <=500 :
                cost = "5.05"

if Shipping == "2" :
        if Size == "2" :
            if weight <= 100 :
                cost = "1.65"
            elif weight  >101 <=200 :
                cost = "2.68"
            elif weight  >=201 <=300 :
                cost = "3.76"
            elif weight  >=301 <=400 :
                cost = "4.27"
            elif weight  >=401 <=500 :
                cost = "4.58"



   #Print the answer

print("This shipment will cost you ${0:.2f}".format(cost)) 

main()

【问题讨论】:

    标签: python string


    【解决方案1】:

    您使用int(...) 将'Shipping' 和Size 转换为整数类型,但将其作为字符串类型"1" 进行比较。

    它永远不会匹配,因此成本永远不会设置为任何值。

    当您打印答案时,您不能,因为成本从未设置为任何值。

    要修复它,请将 Shipping 保留为字符串:

    Shipping = input("Shipping: Enter (1) stamps or (2) for meter postage: ")
    

    或者用数字做测试:

    if Shipping == 1
    

    关于大小的相同更改。体重看起来还可以。

    【讨论】:

      猜你喜欢
      • 2013-06-02
      • 1970-01-01
      • 2015-09-09
      • 2020-08-11
      • 1970-01-01
      • 1970-01-01
      • 2011-06-27
      • 1970-01-01
      • 2016-04-30
      相关资源
      最近更新 更多