首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么我总是在python中得到一个未定义的变量错误消息?

为什么我总是在python中得到一个未定义的变量错误消息?
EN

Stack Overflow用户
提问于 2022-12-03 21:10:59
回答 1查看 22关注 0票数 0

因此,我不断地收到没有定义cost_delivery的错误消息,但是其余的cost__变量都是正常的,我是如何编写它们的呢?我包括我的代码下面-任何帮助将不胜感激!

代码语言:javascript
运行
复制
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非常陌生,所以任何建议都会有帮助!!

EN

回答 1

Stack Overflow用户

发布于 2022-12-03 21:15:45

在以下部分

代码语言:javascript
运行
复制
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条件下对其进行了比较,所以会遇到此错误。

你的代码应该是

代码语言:javascript
运行
复制
delivery = input("Would you like priority or standard delivery?")
if delivery == 'priority':
    cost_delivery = priority_delivery
elif delivery == 'standard':
    cost_delivery = standard_delivery
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74670753

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档