python - NameError:第 13 行中的名称“国内”未定义
问题描述
为长代码道歉,因为每当我调用 main() 函数时它都是相关的。我无法理解如何解决此错误,因为我认为我在 def 函数 firstPackageUserChoice 中将其定义为由 main 回调。
我不得不拿走一大块代码,但希望它仍然有助于为我正在做的事情提供一些背景信息。
提前致谢!
print( "Hello! We are going to determine the cost for shipping two packages" )
#Asking user for input on package weight
def userInput():
package1 = print( int( input( "Please enter the weight of your first package: " ) ) )
choice1 = input ( "Domestic or international for the first package: " )
package2 = print ( int( input( "Please enter the weight of your second package: " ) ) )
choice2 = input ( "Domestic or international for the second package: " )
return package1, package2, choice1, choice2
#First package weight and shipping cost
def firstPackageUserChoice(package1, choice1):
if choice1 == domestic:
if package1 <= 2:
domestic1 = 1.50
print ( f"The cost of the first package is: ${domestic1:,.2f}" )
elif package1 > 2 and package1 <= 6:
domestic1 = 3.00
print ( f"The cost of the first package is: ${domestic1:,.2f}" )
elif package1 > 6 and package1 <= 10:
domestic1 = 4.00
print ( f"The cost of the first package is: ${domestic1:,.2f}" )
else:
package1 > 10
domestic1 = 4.75
print ( f"The cost of the first package is: ${domestic1:,.2f}" )
else:
choice1 = international
if package1 <= 2:
international1 = 5.00
print ( f"The cost of the first package is: ${international1:,.2f}" )
elif package1 > 2 and package1 <= 6:
International1 = 10.00
print ( f"The cost of the first package is: ${international1:,.2f}" )
elif package1 > 6 and package1 <= 10:
international1 = 15.00
print ( f"The cost of the first package is: ${international1:,.2f}" )
else:
package1 > 10
international1 = 25.00
print ( f"The cost of the first package is: ${international1:,.2f}" )
return package1, choice1
解决方案
你需要要么
- 在你的定义
domestic
中定义 - 像
domestic
使用package1
,choice1
\
或 - 创建
domestic
一个全局变量
或 - 是
domestic
一个函数,而您只是忘记包含了()
吗?
或者 - 是
domestic
一个字符串 ==> 需要'domestic'
该选择取决于多种因素,包括是否domestic
被此定义更改。
推荐阅读
- visual-studio-code - Mac 上的 VSCode 和 Oh My Zsh - 如何在终端打开时自动运行命令?
- javascript - 为什么 Node.js 中的 util.inherits() 方法使用 Object.setPrototypeOf()?
- node.js - 如何使用 dotenv 从 .env 和 .env.local 加载环境变量?
- javascript - 为什么这个 Javascript API 会失败?
- postgresql - PostgreSQL。心跳无法正常工作
- asp.net-core - SmtpClient 发送电子邮件错误“邮箱不可用。服务器响应为:5.7.1 客户端无权作为此发件人发送”
- google-cloud-firestore - Firestore - 您的文档应包含少于 100 个字段
- shell - 如何按宽高比对图像进行排序
- python - _tkinter.TclError:错误的事件类型或键符“向上”
- node.js - 在后端使用一些条件语句从 MongoDB 获取数据