我需要帮助我得到的错误是没有定义成本。本程序根据运输类型和重量计算邮政费率。我不确定我做错了什么建议会非常感谢
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()
答案 0 :(得分:0)
您转换&#39;运费&#39;和大小为int(...)
的整数类型,但您将其作为字符串类型"1"
进行比较。
它永远不会匹配,因此成本永远不会被设置为任何东西。
当你打印答案时,你不能,因为费用从未设定为任何东西。
要解决此问题,请将装运保留为字符串:
Shipping = input("Shipping: Enter (1) stamps or (2) for meter postage: ")
或用数字进行测试:
if Shipping == 1
关于尺寸的相同变化。重量看起来不错。