【问题标题】:name 'endCol' is not defined in python script名称“endCol”未在 python 脚本中定义
【发布时间】:2023-04-03 13:45:01
【问题描述】:

我不知道为什么我的变量没有定义

我的代码:

    def menu():
            print("Please select the following option:\n 1. 1b\n 2. 2b\n 3. 3b\n 4. 4b\n 5. 5b\n")
            option = input()
            if option == "1":
                    endCol = 133
            if option == "2":
                    endCol = 135
            if option == "3":
                    endCol = 263
            if option == "4":
                    endCol = 519
            if option == "5":
                    endCol = 1031
    def filebrowser(ext=""):
            "Returns files with an extension"
            return [f for f in glob.glob(f"*{ext}")]
    menu()
    x = filebrowser(".csv")
    csv = input()
    df2 = pd.read_csv(csv, skiprows = range(62,125), usecols = range(3,endCol))

输出:

请选择以下选项:

  1. 1b
  2. 2b
  3. 3b
  4. 4b
  5. 5b

3(输入)

['abc.csv', 'def.csv', 'ghi.csv']

def.csv(输入)

NameError: name 'endCol' 未定义

【问题讨论】:

    标签: python function variables


    【解决方案1】:

    endColmenu函数中的一个局部变量,所以不能从外部使用。您可以返回您需要的值并保存以供以后使用:

    def menu():
            print("Please select the following option:\n 1. 1b\n 2. 2b\n 3. 3b\n 4. 4b\n 5. 5b\n")
            option = input()
            if option == "1":
                    return 133
            if option == "2":
                    return 135
            if option == "3":
                    return 263
            if option == "4":
                    return 519
            if option == "5":
                    return 1031
    def filebrowser(ext=""):
            "Returns files with an extension"
            return [f for f in glob.glob(f"*{ext}")]
    endCol = menu()
    x = filebrowser(".csv")
    csv = input()
    df2 = pd.read_csv(csv, skiprows = range(62,125), usecols = range(3,endCol))
    

    【讨论】:

      【解决方案2】:

      您的变量 endCol 仅在您的函数 menu 中可见,您应该从您的函数中返回 endCol

      def menu():
          print("Please select the following option:\n 1. 1b\n 2. 2b\n 3. 3b\n 4. 4b\n 5. 5b\n")
          option = input()
          endCol = None
          if option == "1":
                  endCol = 133
          if option == "2":
                  endCol = 135
          if option == "3":
                  endCol = 263
          if option == "4":
                  endCol = 519
          if option == "5":
                  endCol = 1031
          return endCol
      
      endCol = menu()
      

      【讨论】:

      • 非常感谢 kederrac。它解决了问题:)
      【解决方案3】:

      您必须(例如)在menu() 函数的最后一个if 语句之后添加一个return endCol 并在最后捕获返回值:

      def menu():
          print("Please select the following option:\n 1. 1b\n 2. 2b\n 3. 3b\n 4. 4b\n 5. 5b\n")
          option = input()
          if option == "1":
              endCol = 133
          if option == "2":
              endCol = 135
          if option == "3":
              endCol = 263
          if option == "4":
              endCol = 519
          if option == "5":
              endCol = 1031
          return endCol
      
      ## when calling the menu() function:
      V = menu()
      
      ### you can now work with the value V:
      
      df2 = pd.read_csv(csv, skiprows=range(62,125), usecols=range(3, V))
      

      【讨论】:

        猜你喜欢
        • 2014-10-22
        • 1970-01-01
        • 1970-01-01
        • 2021-05-31
        • 2022-06-14
        • 2022-11-27
        • 2012-01-31
        • 2023-03-04
        • 1970-01-01
        相关资源
        最近更新 更多