Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List Help
#4
(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
Reply


Messages In This Thread
List Help - by slackerman73 - Dec-08-2019, 09:51 PM
RE: List Help - by ichabod801 - Dec-08-2019, 11:02 PM
RE: List Help - by slackerman73 - Dec-12-2019, 03:58 AM
RE: List Help - by ichabod801 - Dec-12-2019, 02:25 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020