How about using a dictionary instead of two separate lists/tuples:
>>> user_dict = {}
>>> user_dict['user1'] = 'passwd1'
>>> user_dict.get('user2')
>>> user_dict.get('user1')
'passwd1'
>>>
As you can see, by using the get function, we can test whether a user name is in the dictionary or not. If the name is in the dictionary, it returns a string; otherwise it returns a None .
I think this would simplify and speed up your code big time.
jlm699
Veteran Poster
1,112 posts since Jul 2008
Reputation Points: 355
Solved Threads: 292
the reason it's coming up as match is that they are equal so i suggest declaring ind1, ind2 and making one 0 and the other 1
[code = python]
users = ('user1','user2','user3')
passw = ('pass1','pass2','pass3')
ind1,ind2 = 0,1
[/code]
baki100
Junior Poster in Training
79 posts since Apr 2009
Reputation Points: 12
Solved Threads: 14
You can concatenate tuples, the trick is to wrote the one element tuple correctly:
users = ('user1','user2','user3')
# from input ...
new_user = 'user4'
# concatenate tuples, notice the way a one element tuple is written
users = users + (new_user, )
print users # ('user1', 'user2', 'user3', 'user4')
Ene Uran
Posting Virtuoso
1,723 posts since Aug 2005
Reputation Points: 625
Solved Threads: 213
Concerning your original code, you declare ind1 and ind2 beneath the if (), so keep them there
#-----------------------------------------------
if user_in in users:
users = list(users)
passw = list(passw)
ind1 = users.index(user_in)
ind2 = passw.index(passw_in)
users = tuple(users)
passw = tuple(passw)
## but if neither user name or password is found,
## then ind1 == ind2 == -1
if (ind1 > -1) and (ind1 == ind2):
print('Match')
else:
print('Not Match')
I would think though that the point here is not to convert back and forth from lists, but to use a for() loop to iterate through the tuple(s). You can do this two ways, (1) use a separate tuple for name and password, or (2) use a nested tuple of tuples containing both name and password, It doesn't really matter which one you choose.
#----------------------------
def user_passw_1tuple(users_and_passw):
user_in = raw_input('Username? ')
user_in = user_in.strip()
for ctr in range(0, len(users)):
if user_in == users_and_passw[ctr][0]:
passw_in = raw_input('Password? ')
if passw_in == users_and_passw[ctr][1]:
return True
return False
#----------------------------
def user_passw_2tuples(users, passw):
user_in = raw_input('Username? ')
user_in = user_in.strip()
for ctr in range(0, len(users)):
if user_in == users[ctr]:
passw_in = raw_input('Password? ')
if passw_in == passw[ctr]:
return True
return False
##-------------------------------------------------------------------------------
users = ('user1','user2','user3')
passw = ('pass1','pass2','pass3')
users_passw = (('user1', 'pass1'), ('user2', 'pass2'),
('user3', 'pass3'))
result = user_passw_1tuple(users_passw)
if result:
print("Match for '1tuple'\n")
else:
print("No Match for '1tuple'\n")
result = user_passw_2tuples(users, passw)
if result:
print("\nMatch for '2tuples'")
else:
print("\nNo Match for '2tuples'")
You will now have to come up with a function to add a user as well.
woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714
you could use zip...
>>> users = ('bob', 'carl', 'edna')
>>> passwords = ('1234', 'pass', 'icu812')
>>> zip(users, passwords)
[('bob', '1234'), ('carl', 'pass'), ('edna', 'icu812')]
>>> mydict = dict(zip(users, passwords))
>>> mydict['bob']
'1234'
>>> dict(zip(users, passwords))['bob']
'1234'
or you could use indexes:
>>> passwords[users.index('bob')]
'1234'
u can call it directly if u really wanted to.... not sure what the rules for the assignment were.
Tuples cannot be changed easily... they are faster than lists, but really not that great for writing to... you could do this though:
>>> users = list(users)
>>> users
['bob', 'carl', 'edna']
>>> users.append('steve')
>>> users = tuple(users)
>>> users
('bob', 'carl', 'edna', 'steve')
>>>
You can list it, append it, then tuple again without using seperate variables, it wont compromise the list/tuple.
lukerobi
Junior Poster in Training
50 posts since Sep 2009
Reputation Points: 14
Solved Threads: 16
As your users and their passwords get more numerous, it will be difficult to keep them matched in separate containers like tuples. So I would go with user/password pairs like was suggested above. Either a tuple of (user, password) tuples or a user:password dictionary. The dictionary would be easier to maintain, when you want to add or remove users.
You would normally cloak your passwords using some kind of encoding and decoding algorithm, a fair number of those are built-into Python.
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417