Ok, I'm pretty new to Python....

What I need to do is set up a couple tuples like so:

users = ('user1','user2','user3')
passw = ('pass1','pass2','pass3')

I then am asking the user for their username and password to compare to the tuples. The only way I could think to do that was converting the tuples to list and using the index() then assigning that index to a var and the converting it back to a tuple:

users = list(users)
   passw = list(passw)
   ind1 = users.index(user_in)
   ind2 = passw.index(passw_in)
   users = tuple(users)
   passw = tuple(passw)

then using an if/else print the necessarily things, but this just seems a little arbitrary....redundant... I dont know, something...Is there an easier way to go about?

This is my entire script so far:

#------------------------------
users =  ('user1','user2','user3')
passw = ('pass1','pass2','pass3')


#----------------------------
user_in = raw_input('Username? ')
passw_in = raw_input('Password? ')


#-----------------------------------  
if not (user_in in users):
   new_user = raw_input('User "%s" not found - would you like to enroll? ' %user_in)

if new_user == ('y' or 'Y'):
      users = list(users)
      passw = list(passw)
      users.append(user_in)
      passw.append(passw_in)
      users = tuple(users)
      passw = tuple(passw)

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

if ind1 == ind2:
   print('Match')
elif ind1 != ind2:
   print('Not Match'
)

It seems to work except if when the user puts in a non registerd username and decides let say "n" to enroll it still assigns ind1 and ind2 the value 3 and prints "Match"

Thanks for any help

Recommended Answers

All 9 Replies

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.

That looks a lot more efficient, though unfortunately this is an assignment and was specifically told to use tuples. But if I wanted to run with what you had, how would i use the get function to then compare it to the user's inputs. I tired playing around w/ it to see if I could figure it out..but no go.. never done dictionaries.

Also I tried assigning 3 users and 3 passwords to the dictionary, if i try to use the get function it doesn't return anything.

Enter Username: user1
Password: pass1
Not Valid
>>> user_dict
{('user1', 'user2', 'user3'): ('pass1', 'pass2', 'pass3')}
>>> user_dict.get('user1')
>>>

Thanks

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

users =  ('user1','user2','user3')
passw = ('pass1','pass2','pass3')
ind1,ind2 = 0,1

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

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

users =  ('user1','user2','user3')
passw = ('pass1','pass2','pass3')
ind1,ind2 = 0,1

Now I get "No match"...i think its cause of my if/else...How can get that the only follow my previous if statement saying:

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)

Thanks

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

users =  ('user1','user2','user3')
passw = ('pass1','pass2','pass3')
ind1,ind2 = 0,1

That also seems like a more effective way of doing things, thanks.
But how would you compare the values, lets say if user1 wanted to login. I have to verify he puts in his password and not the password of user2.

Thanks

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.

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.

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.

Thanks for all the great replies and help...I have a good idea of how I want to go about it. Again, Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.