| | |
Compare Tuple Values
Thread Solved |
•
•
Join Date: Sep 2009
Posts: 6
Reputation:
Solved Threads: 0
Ok, I'm pretty new to Python....
What I need to do is set up a couple tuples like so:
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:
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:
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
What I need to do is set up a couple tuples like so:
Python Syntax (Toggle Plain Text)
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:
Python Syntax (Toggle Plain Text)
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:
Python Syntax (Toggle Plain Text)
#------------------------------ 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
Last edited by khaos64; Sep 29th, 2009 at 12:59 am.
How about using a dictionary instead of two separate lists/tuples:
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
I think this would simplify and speed up your code big time.
python Syntax (Toggle Plain Text)
>>> user_dict = {} >>> user_dict['user1'] = 'passwd1' >>> user_dict.get('user2') >>> user_dict.get('user1') 'passwd1' >>>
None .I think this would simplify and speed up your code big time.
•
•
Join Date: Sep 2009
Posts: 6
Reputation:
Solved Threads: 0
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.
Thanks
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)
Enter Username: user1 Password: pass1 Not Valid >>> user_dict {('user1', 'user2', 'user3'): ('pass1', 'pass2', 'pass3')} >>> user_dict.get('user1') >>>
Thanks
You can concatenate tuples, the trick is to wrote the one element tuple correctly:
python Syntax (Toggle Plain Text)
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')
drink her pretty
•
•
Join Date: Sep 2009
Posts: 6
Reputation:
Solved Threads: 0
•
•
•
•
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]
Python Syntax (Toggle Plain Text)
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
[code = python]
users = ('user1','user2','user3')
passw = ('pass1','pass2','pass3')
ind1,ind2 = 0,1
[/code]
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
•
•
Join Date: Dec 2006
Posts: 1,008
Reputation:
Solved Threads: 285
Concerning your original code, you declare ind1 and ind2 beneath the if (), so keep them there 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. You will now have to come up with a function to add a user as well.
Python Syntax (Toggle Plain Text)
#----------------------------------------------- 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')
Python Syntax (Toggle Plain Text)
#---------------------------- 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'")
Last edited by woooee; Sep 29th, 2009 at 9:37 pm.
Linux counter #99383
•
•
Join Date: Sep 2009
Posts: 50
Reputation:
Solved Threads: 16
you could use zip...
or you could use indexes:
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:
You can list it, append it, then tuple again without using seperate variables, it wont compromise the list/tuple.
Python Syntax (Toggle Plain Text)
>>> 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:
Python Syntax (Toggle Plain Text)
>>> 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:
Python Syntax (Toggle Plain Text)
>>> 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.
Last edited by lukerobi; Sep 29th, 2009 at 11:55 pm.
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.
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!
![]() |
Similar Threads
- php: compare array values from 2 tables (PHP)
- Comparisons of Array Values to X (C)
- compare cell values (ASP.NET)
- Comparing two files and output values that match (Shell Scripting)
- JDBC compare the content of two tables (Java)
- compare values in a tuple/list (Python)
- Comparing Random generated values with Entered values in LogIn screen (ASP.NET)
- Super-power compare method? <?> (Java)
Other Threads in the Python Forum
- Previous Thread: GIS Geoprocessing
- Next Thread: Python Coding
| Thread Tools | Search this Thread |
abrupt accessdenied anti apache application approximation argv array beginner book builtin calculator change converter countpasswordentry curved dan08 dictionaries dictionary dynamic edit enter examples file float format function gui homework import inches input java keyboard lapse launcher library line lines linux list lists loop microphone mouse movingimageswithpygame mysqlquery newb number numbers numeric output parameters parsing path phonebook plugin port prime programming projects py2exe pygame pyopengl pysimplewizard python random recursion redirect remote reverse scrolledtext session simple software sprite statictext string strings syntax table terminal text textarea thread threading time tlapse trick tuple tutorial twoup ubuntu unicode unit urllib urllib2 variable wordgame wxpython






