Compare Tuple Values

Thread Solved

Join Date: Sep 2009
Posts: 6
Reputation: khaos64 is an unknown quantity at this point 
Solved Threads: 0
khaos64 khaos64 is offline Offline
Newbie Poster

Compare Tuple Values

 
0
  #1
Sep 29th, 2009
Ok, I'm pretty new to Python....

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

  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:
  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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,046
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 264
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: Compare Tuple Values

 
0
  #2
Sep 29th, 2009
How about using a dictionary instead of two separate lists/tuples:
  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.
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 6
Reputation: khaos64 is an unknown quantity at this point 
Solved Threads: 0
khaos64 khaos64 is offline Offline
Newbie Poster

Re: Compare Tuple Values

 
0
  #3
Sep 29th, 2009
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.
  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
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 44
Reputation: baki100 is an unknown quantity at this point 
Solved Threads: 10
baki100 baki100 is offline Offline
Light Poster

Re: Compare Tuple Values

 
0
  #4
Sep 29th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,542
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 171
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: Compare Tuple Values

 
0
  #5
Sep 29th, 2009
You can concatenate tuples, the trick is to wrote the one element tuple correctly:
  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')
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 6
Reputation: khaos64 is an unknown quantity at this point 
Solved Threads: 0
khaos64 khaos64 is offline Offline
Newbie Poster

Re: Compare Tuple Values

 
0
  #6
Sep 29th, 2009
Originally Posted by baki100 View Post
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:
  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
Originally Posted by baki100 View Post
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,008
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 285
woooee woooee is offline Offline
Veteran Poster

Re: Compare Tuple Values

 
0
  #7
Sep 29th, 2009
Concerning your original code, you declare ind1 and ind2 beneath the if (), so keep them there
  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.
  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.
Linux counter #99383
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 50
Reputation: lukerobi is an unknown quantity at this point 
Solved Threads: 16
lukerobi lukerobi is offline Offline
Junior Poster in Training

Re: Compare Tuple Values

 
0
  #8
Sep 29th, 2009
you could use zip...

  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:
  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:

  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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,013
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 929
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Compare Tuple Values

 
0
  #9
Sep 30th, 2009
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.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 6
Reputation: khaos64 is an unknown quantity at this point 
Solved Threads: 0
khaos64 khaos64 is offline Offline
Newbie Poster

Re: Compare Tuple Values

 
0
  #10
Oct 1st, 2009
Thanks for all the great replies and help...I have a good idea of how I want to go about it. Again, Thanks.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC