Python 名称错误:名称';国内';第13行中没有定义
很抱歉代码太长,因为无论何时调用main()函数,它都是相关的。 我很难理解如何解决这个错误,因为我认为我在def函数firstPackageUserChoice中定义了它,main将调用它 我不得不去掉一大块代码,但希望它仍能帮助我理解我正在做的事情 提前谢谢Python 名称错误:名称';国内';第13行中没有定义,python,Python,很抱歉代码太长,因为无论何时调用main()函数,它都是相关的。 我很难理解如何解决这个错误,因为我认为我在def函数firstPackageUserChoice中定义了它,main将调用它 我不得不去掉一大块代码,但希望它仍能帮助我理解我正在做的事情 提前谢谢 print( "Hello! We are going to determine the cost for shipping two packages" ) #Asking user for input on pa
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
print(“您好!我们将确定运送两个包裹的成本”)
#要求用户输入包装重量
def userInput():
package1=打印(int(输入(“请输入您的第一个包裹的重量:”))
选择1=输入(“第一个包的国内或国际:”)
package2=打印(int(输入(“请输入第二个包裹的重量:”))
choice2=输入(“第二个包的国内或国际:”)
退货包装1,包装2,选择1,选择2
#首件包装重量和运输成本
def firstPackageUserChoice(package1,选项1):
如果choice1==国内:
如果包装1 2和包装1 6和包装1 10
家庭1=4.75
打印(f“第一包的成本为:${domestic1:,.2f}”)
其他:
选择1=国际
如果包装1 2和包装1 6和包装1 10
国际1=25.00
打印(f“第一个包的成本为:${international1:,.2f}”)
退货包装1,选择1
您需要
国内
国内
传递到您的函数中,就像您使用package1
,choice1
\或
国内
设为全局变量或
是国内的
功能,而您只是忘记了包含()
或
国内
是一个字符串==>需要国内
这一选择取决于多种因素,包括
国内
是否因该定义而改变。您在第13行使用了一个未定义的变量,您不是想将作为字符串键入“国内”
吗?对不起,这意味着要写为“国内1”。我现在看到了我的错误,但得到了一个UnboundLocalError:在赋值之前引用了局部变量'domestic1'。编辑:我很快就会弄明白的。谢谢你指出这一点,也谢谢大卫的帮助。@JDL98,很高兴它起了作用,欢迎来到SO。请阅读以下内容,了解投票和接受有用答案的重要性:。用答案左上角的箭头记录投票,点击答案左上角的复选标记记录接受。谢谢