因此,我不断地收到没有定义cost_delivery的错误消息,但是其余的cost__变量都是正常的,我是如何编写它们的呢?我包括我的代码下面-任何帮助将不胜感激!
price = float(input("Please enter the price of the package: "))
distance = float(input("Please enter the distance of delivery in kms: "))
air = 0.36
freight = 0.25
travel = input("Would you like to send your package via air or freight? ")
if travel == 'air':
cost_travel = air * distance
elif travel == 'frieght':
cost_travel = freight * distance
else:
print("Error. Enter either air or frieght.")
full_insurance = 50
lim_insurance = 25
insurance = input("Would you like full insurance or partial insurance?")
if insurance == 'full insurance':
cost_insurance = full_insurance
elif insurance == 'partial insurance':
cost_insurance = lim_insurance
else:
print("Error. Either enter full insurance or partial insurance.")
inc_gift = 15
no_gift = 0
gift = input("Would you like to include gift wrapping?")
if gift == 'yes':
cost_gift = inc_gift
elif gift == 'no':
cost_gift = no_gift
else:
print("Either enter yes or no.")
priority_delivery = 100
standard_delivery = 20
delivery = input("Would you like priority or standard delivery?")
if delivery == 'priority':
cost_delivery == priority_delivery
elif delivery == 'standard':
cost_delivery == standard_delivery
total_cost = cost_travel + cost_insurance + cost_gift + cost_delivery
print(total_cost)我试图在标准和优先级传递定义下定义cost_delivery =0,但最终计算中根本没有包括交付成本。
我对python非常陌生,所以任何建议都会有帮助!!
发布于 2022-12-03 21:15:45
在以下部分
delivery = input("Would you like priority or standard delivery?")
if delivery == 'priority':
cost_delivery == priority_delivery
elif delivery == 'standard':
cost_delivery == standard_delivery==用于检查两个对象是否具有相同的值。
由于上面没有在代码中定义cost_delivery,并且在if .. elif条件下对其进行了比较,所以会遇到此错误。
你的代码应该是
delivery = input("Would you like priority or standard delivery?")
if delivery == 'priority':
cost_delivery = priority_delivery
elif delivery == 'standard':
cost_delivery = standard_deliveryhttps://stackoverflow.com/questions/74670753
复制相似问题