943,545 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 828
  • Python RSS
Sep 29th, 2009
0

Compare Tuple Values

Expand Post »
Ok, I'm pretty new to Python....

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

Python Syntax (Toggle Plain Text)
  1. users = ('user1','user2','user3')
  2. 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:
Python Syntax (Toggle Plain Text)
  1. users = list(users)
  2. passw = list(passw)
  3. ind1 = users.index(user_in)
  4. ind2 = passw.index(passw_in)
  5. users = tuple(users)
  6. 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:
Python Syntax (Toggle Plain Text)
  1. #------------------------------
  2. users = ('user1','user2','user3')
  3. passw = ('pass1','pass2','pass3')
  4.  
  5.  
  6. #----------------------------
  7. user_in = raw_input('Username? ')
  8. passw_in = raw_input('Password? ')
  9.  
  10.  
  11. #-----------------------------------
  12. if not (user_in in users):
  13. new_user = raw_input('User "%s" not found - would you like to enroll? ' %user_in)
  14.  
  15. if new_user == ('y' or 'Y'):
  16. users = list(users)
  17. passw = list(passw)
  18. users.append(user_in)
  19. passw.append(passw_in)
  20. users = tuple(users)
  21. passw = tuple(passw)
  22.  
  23. #-----------------------------------------------
  24. if user_in in users:
  25. users = list(users)
  26. passw = list(passw)
  27. ind1 = users.index(user_in)
  28. ind2 = passw.index(passw_in)
  29. users = tuple(users)
  30. passw = tuple(passw)
  31.  
  32. if ind1 == ind2:
  33. print('Match')
  34. elif ind1 != ind2:
  35. print('Not Match'
  36. )

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
Last edited by khaos64; Sep 29th, 2009 at 12:59 am.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
khaos64 is offline Offline
7 posts
since Sep 2009
Sep 29th, 2009
0

Re: Compare Tuple Values

How about using a dictionary instead of two separate lists/tuples:
python Syntax (Toggle Plain Text)
  1. >>> user_dict = {}
  2. >>> user_dict['user1'] = 'passwd1'
  3. >>> user_dict.get('user2')
  4. >>> user_dict.get('user1')
  5. 'passwd1'
  6. >>>
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.
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008
Sep 29th, 2009
0

Re: Compare Tuple Values

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.
Python Syntax (Toggle Plain Text)
  1. Enter Username: user1
  2. Password: pass1
  3. Not Valid
  4. >>> user_dict
  5. {('user1', 'user2', 'user3'): ('pass1', 'pass2', 'pass3')}
  6. >>> user_dict.get('user1')
  7. >>>

Thanks
Reputation Points: 10
Solved Threads: 0
Newbie Poster
khaos64 is offline Offline
7 posts
since Sep 2009
Sep 29th, 2009
0

Re: Compare Tuple Values

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]
Last edited by baki100; Sep 29th, 2009 at 10:18 am.
Reputation Points: 12
Solved Threads: 14
Junior Poster in Training
baki100 is offline Offline
79 posts
since Apr 2009
Sep 29th, 2009
0

Re: Compare Tuple Values

You can concatenate tuples, the trick is to wrote the one element tuple correctly:
python Syntax (Toggle Plain Text)
  1. users = ('user1','user2','user3')
  2.  
  3. # from input ...
  4. new_user = 'user4'
  5.  
  6. # concatenate tuples, notice the way a one element tuple is written
  7. users = users + (new_user, )
  8.  
  9. print users # ('user1', 'user2', 'user3', 'user4')
Reputation Points: 625
Solved Threads: 211
Posting Virtuoso
Ene Uran is offline Offline
1,704 posts
since Aug 2005
Sep 29th, 2009
0

Re: Compare Tuple Values

Click to Expand / Collapse  Quote originally posted by baki100 ...
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]
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:
Python Syntax (Toggle Plain Text)
  1. if user_in in users:
  2. users = list(users)
  3. passw = list(passw)
  4. ind1 = users.index(user_in)
  5. ind2 = passw.index(passw_in)
  6. users = tuple(users)
  7. passw = tuple(passw)

Thanks
Click to Expand / Collapse  Quote originally posted by baki100 ...
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]
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
khaos64 is offline Offline
7 posts
since Sep 2009
Sep 29th, 2009
0

Re: Compare Tuple Values

Concerning your original code, you declare ind1 and ind2 beneath the if (), so keep them there
Python Syntax (Toggle Plain Text)
  1. #-----------------------------------------------
  2. if user_in in users:
  3. users = list(users)
  4. passw = list(passw)
  5. ind1 = users.index(user_in)
  6. ind2 = passw.index(passw_in)
  7. users = tuple(users)
  8. passw = tuple(passw)
  9.  
  10. ## but if neither user name or password is found,
  11. ## then ind1 == ind2 == -1
  12. if (ind1 > -1) and (ind1 == ind2):
  13. print('Match')
  14. else:
  15. 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.
Python Syntax (Toggle Plain Text)
  1. #----------------------------
  2. def user_passw_1tuple(users_and_passw):
  3. user_in = raw_input('Username? ')
  4. user_in = user_in.strip()
  5. for ctr in range(0, len(users)):
  6. if user_in == users_and_passw[ctr][0]:
  7. passw_in = raw_input('Password? ')
  8. if passw_in == users_and_passw[ctr][1]:
  9. return True
  10. return False
  11.  
  12. #----------------------------
  13. def user_passw_2tuples(users, passw):
  14. user_in = raw_input('Username? ')
  15. user_in = user_in.strip()
  16. for ctr in range(0, len(users)):
  17. if user_in == users[ctr]:
  18. passw_in = raw_input('Password? ')
  19. if passw_in == passw[ctr]:
  20. return True
  21. return False
  22.  
  23.  
  24. ##-------------------------------------------------------------------------------
  25. users = ('user1','user2','user3')
  26. passw = ('pass1','pass2','pass3')
  27.  
  28. users_passw = (('user1', 'pass1'), ('user2', 'pass2'),
  29. ('user3', 'pass3'))
  30.  
  31. result = user_passw_1tuple(users_passw)
  32. if result:
  33. print("Match for '1tuple'\n")
  34. else:
  35. print("No Match for '1tuple'\n")
  36.  
  37. result = user_passw_2tuples(users, passw)
  38. if result:
  39. print("\nMatch for '2tuples'")
  40. else:
  41. print("\nNo Match for '2tuples'")
You will now have to come up with a function to add a user as well.
Last edited by woooee; Sep 29th, 2009 at 9:37 pm.
Reputation Points: 741
Solved Threads: 691
Nearly a Posting Maven
woooee is online now Online
2,302 posts
since Dec 2006
Sep 29th, 2009
0

Re: Compare Tuple Values

you could use zip...

Python Syntax (Toggle Plain Text)
  1. >>> users = ('bob', 'carl', 'edna')
  2. >>> passwords = ('1234', 'pass', 'icu812')
  3. >>> zip(users, passwords)
  4. [('bob', '1234'), ('carl', 'pass'), ('edna', 'icu812')]
  5. >>> mydict = dict(zip(users, passwords))
  6. >>> mydict['bob']
  7. '1234'
  8. >>> dict(zip(users, passwords))['bob']
  9. '1234'

or you could use indexes:
Python Syntax (Toggle Plain Text)
  1. >>> passwords[users.index('bob')]
  2. '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:

Python Syntax (Toggle Plain Text)
  1. >>> users = list(users)
  2. >>> users
  3. ['bob', 'carl', 'edna']
  4. >>> users.append('steve')
  5. >>> users = tuple(users)
  6. >>> users
  7. ('bob', 'carl', 'edna', 'steve')
  8. >>>

You can list it, append it, then tuple again without using seperate variables, it wont compromise the list/tuple.
Last edited by lukerobi; Sep 29th, 2009 at 11:55 pm.
Reputation Points: 14
Solved Threads: 16
Junior Poster in Training
lukerobi is offline Offline
50 posts
since Sep 2009
Sep 30th, 2009
0

Re: Compare Tuple Values

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.
Last edited by vegaseat; Sep 30th, 2009 at 12:32 pm.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Oct 1st, 2009
0

Re: Compare Tuple Values

Thanks for all the great replies and help...I have a good idea of how I want to go about it. Again, Thanks.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
khaos64 is offline Offline
7 posts
since Sep 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: GIS Geoprocessing
Next Thread in Python Forum Timeline: Python Coding





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC