user = ['chew','joshton']
password= [1234,2234]
if i added a new element to ‘user’ and ‘password’,how can i make it so when i wanted to log in as chew my needed password is 1234,and after a new account appears in the list as the first element and chew as the second the password is the same ?such as:
user = ['test','chew','joshton']
password,[3214,1234,2234]
i made a function called number_order()which finds out an elements number order such as 1,2,3,….
but i cant use it as the computer wont know which in the list ‘user’am i reffering to
im thinking about some thing like number_order(‘chew’, list) and smt thatll make the computer know which one in the list ‘user’am i referring to ?i cant hard code it like if which_user ==’chew’ elif …
as this list will be changed when the admin wants to .
I heard from a friend that dict() will work but i dont know how to utilize it,pls do give some suggestions ill try my best
>Solution :
Using a dictionary would be better because it’s a structured, associative data structure. This means that you put stuff together that belongs together as opposed to having them spread out over several lists (which you have to keep in sync).
However, if you don’t want to use a dictionary, you can do something like this:
user = ['test', 'chew', 'joshton']
password = [3214, 1234, 2234]
userenter = input("What is your username: ")
passwordget = int(input("What is your password: "))
count = 0
valuesmatch = False
while count < len(user) and not valuesmatch:
if user[count] == userenter:
if password[count] == passwordget:
valuesmatch = True
count = count + 1
if count == len(user):
print("Username not found or password incorrect")
if valuesmatch:
print("Correct password and username")
you could also use user.index, as suggested by Friedrich below:
user = ['test', 'chew', 'joshton']
password = [3214, 1234, 2234]
userenter = input("What is your username: ")
passwordget = int(input("What is your password: "))
usergot = True
try:
position = user.index(userenter)
except ValueError:
print("Username not found")
usergot = False
if usergot:
if passwordget == password[position]:
print("Username and password match")
valuesmatch = True
else:
print("Incorrect password")
Both of these, and also a dictionary will only work if there is one person with that username/primary key.