I'm building my 1st Python app, a car service where clients can request vehicles for transportation.
I have a "Clients.txt" file (in the same folder) with the Usernames and Passwords of each client, and I've successfully created the function "read_clients()" that opens the said file and stores each username in "client_username" and each password in "client_password".
What I want to do now is create an object in the Class "Client" for every client, and automatically make each object's self.username = client_username and each object's self.password = client_password.
I tried using a for loop. I think I shouldn't call each object c1, as I think this would just keep overwriting the c1 variable, right? But what's the solution? Should I implement some "counter" variable and increase it by 1 with each cycle and then create the variable c"counter"?
Should I use a dictionary for this instead? Maybe not, because I have a series of functions like create_account() and login() which I'll want to run on each object's variables. Right?
Thanks!
I tried defining the read_clients function both inside and outside of the "Clients" class and removing and applying the "self" attribute.
class Client:
def __init__(self, username, password):
self.username = username
self.password = password
def read_clients(self):
with open("Clients.txt", "r") as file:
client_list = file.readlines()
for pre_client in client_list:
client = pre_client.split(" ")
client_username = client[0]
pre_password = client[1]
client_password = pre_password.strip("\n")
c1 = Client(client_username, client_password)