首页 > 解决方案 > 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

标签: python

解决方案


你需要要么

  1. 在你的定义domestic中定义
  2. domestic使用package1, choice1\
  3. 创建domestic一个全局变量
  4. domestic一个函数,而您只是忘记包含了()吗?
    或者
  5. domestic一个字符串 ==> 需要'domestic'

该选择取决于多种因素,包括是否domestic被此定义更改。


推荐阅读