0

In my school project I have to make global "list of clients" function so i dont have to open my files everytime i need something from this file in other functions. Here it says that client_list is not defined:

global client_list

def clients():
    client_list = []
    with open("svi.txt","r") as f:
        every_part = ["username","password","name","lastname","role"]
        for r in f.readlines():
            dicct = {}
            bla = r.strip().split("|")
            count = 0
            for i in bla:
                dicct[every_part[count]] = i
                count += 1
            client_list.append(dicct)
def log(file_path,user,password,delimiter):
    for r in client_list:
        n = r.strip().split(delimiter)
        username = n[0]
        passwordd = n[1]
        role = n[4]
        while username == user and passwordd == password:
            if role == "buyer":
                return buyer_menu2()
            elif role == "manager":
                return menager_menu2()
            elif role == "seller":
                return seller_menu2() 
    return False
def login():
    x = False

    while x == False:
        user = input("Username: ")
        password = input("Password: ")
        x = log("svi.txt",user,password,"|")
        if x == False:
            print()
            print("Wrong username or password, please try again. ")
            print()
    print()
login()

2 Answers2

0

As mentioned here, the global keyword in Python is for mutating or defining global variables in a local context. You don't need it to declare global variables in an already global scope, so where you are writing global client_list, you can just define the variable itself by writing something like client_list = []

Where you will need the global keyword is in any function you intend to modify your global client_list variable. So, for example, if you intend for the clients() function to append to (or otherwise mutate) the global client_list, it should include a global client_list declaration.

def clients():
    global client_list
    with open("svi.txt","r") as f:
    <the rest of your function>
Mindful
  • 394
  • 3
  • 13
0

If by "global function", you actually mean "global variable", the following should help:

client_list = []

def clients():
    global client_list
    client_list = []
    with open("svi.txt","r") as f:
        every_part = ["username","password","name","lastname","role"]
        for r in f.readlines():
            dicct = {}
            bla = r.strip().split("|")
            count = 0
            for i in bla:
                dicct[every_part[count]] = i
                count += 1
            client_list.append(dicct)
def log(file_path,user,password,delimiter):
    # No need to declare client_list as global for reading it
    for r in client_list:
        n = r.strip().split(delimiter)
        username = n[0]
        passwordd = n[1]
        role = n[4]
        while username == user and passwordd == password:
            if role == "buyer":
                return buyer_menu2()
            elif role == "manager":
                return menager_menu2()
            elif role == "seller":
                return seller_menu2() 
    return False
def login():
    x = False

    while x == False:
        user = input("Username: ")
        password = input("Password: ")
        x = log("svi.txt",user,password,"|")
        if x == False:
            print()
            print("Wrong username or password, please try again. ")
            print()
    print()
login()

So don't declare client_list as global at the top of your file, but rather in those functions where you want to change its content.

Edit Based on Comment: Of course, you'd have to call clients at least once in your program to initially fill your client_list. This is not done my code because I just took yours and assumed you're doing it somewhere else already.

FERNman
  • 325
  • 2
  • 13