0

I have no idea on why my variable is not defined

My code:

    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))

Output:

Please select the following option:

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

3 (input)

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

def.csv (input)

NameError: name 'endCol' is not defined

3 Answers3

1

endCol is a local variable in menu function, so it can't be used from outside. You can return value you need and save it to use later:

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))
fas
  • 1,393
  • 10
  • 20
0

your variable endCol is visible only from your function menu you should return endCol form your function:

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()
rid
  • 61,078
  • 31
  • 152
  • 193
kederrac
  • 16,819
  • 6
  • 32
  • 55
0

You would have to (for exmaple) add a return endCol after the last if statement of the menu() function and catch the return value in the end:

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))
xtlc
  • 1,070
  • 1
  • 15
  • 41