Dec-12-2019, 02:25 PM
(Dec-12-2019, 03:58 AM)slackerman73 Wrote: Ichabod - I don't have a choice. This is a program for a class I'm taking and the list was defined for me.
Then please post in homework, where it is more expected that you are working under constraints like this.
Look, I type in 'yahoo', because I want that password. That becomes passwordToLookup. You check
passwordToLookup in passwords
. What does that do? It checks if "yahoo" is equal to ["yahoo", "XqffoZeo"] (it isn't, a string is not a list), and then checks if "yahoo" is equal to ["google","CoIushujSetu"] (again, it isn't). Since "yahoo" is not equal to either of the items in passwords, passwordToLookup in passwords
is False, and you never get a match.Now, "yahoo" is equal to the first item in the first item of passwords, so that is what you need to check against. If you haven't gotten to list comprehensions yet, this is how you would do it:
password = '' for site_pass in passwords: # loop through the site/password pairs if passwordToLookup == site_pass[0]: # check if the site is the first item in the site/password pair password = site_pass[1] # set the password from the pair break # stop looking if password: print(password) else: print('Site not found.')If you have covered tuple assignment, you can make this a bit clearer:
password = '' for site, pass in passwords: # loop through the site/password pairs if passwordToLookup == site: # check if the site is the first item in the site/password pair password = spass # set the password from the pair break # stop looking if password: print(password) else: print('Site not found.')
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures