Below is a snippet of my program.
I need to be able to match the user's input to a website and then print the password associated with that website. My initial though was to do an if statement to look for the website and then print the password but that's not working. Any help is much appreciated.
passwords = [["yahoo","XqffoZeo"],["google","CoIushujSetu"]]
print("Which website do you want to lookup the password for?")
for keyvalue in passwords:
passwordToLookup = input()
passwords.index(passwordToLookup)
if keyvalue in passwords:
print ('Yep, its in there.') #I just wrote this to test my code and then I was going to author the code to print the password.
else:
print ('Not in there.')
You only want to ask once, not for each item in the list. So get rid of line 4. Line 7 should be after the conditional on line 9, but neither of them are ever going to find anything. What is teh value of passwordToLookup? It's a string. What are the items of passwords? They are lists. You are never going to match a list to a string.
I would use a dictionary instead of a list:
passwords = {"yahoo": "XqffoZeo", "google": "CoIushujSetu"}
Then you can use the 'in' operator like you are using it:
if passwordToLookup in passwords:
print(passwords[passwordToLookup])
Ichabod - I don't have a choice. This is a program for a class I'm taking and the list was defined for me. I'm simply supposed to write the code to display the password for a given website.
I hate to be so thick about this but I'm just not getting it. I included some instructions from my prof.
passwords = [["yahoo","XqffoZeo"],["google","CoIushujSetu"]]
while True:
print("What would you like to do:")
print(" 1. Open password file")
print(" 2. Lookup a password")
print(" 3. Add a password")
print(" 4. Save password file")
print(" 5. Print the encrypted password list (for testing)")
print(" 6. Quit program")
print(" 7. Delete a password")
print("Please enter a number (1-7)")
choice = input()
if(choice == '2'): #Lookup at password
print("Which website do you want to lookup the password for?")
for keyvalue in passwords:
print(keyvalue[0])
passwordToLookup = input()
if passwordToLookup in passwords:
print(passwords[passwordToLookup])
else:
print ('Not in there.')# If the website isn't in the list it prints, Not in there.
#2. Check if the name is found. To index a list of lists you use 2 square backet sets
# So passwords[0][1] would mean for the first item in the list get it's 2nd item (remember, lists start at 0)
# So this would be 'XqffoZeo' in the password list given what is predefined at the top of the page.
# If you created a loop using the syntax described in step 1, then i is your 'iterator' in the list so you
# will want to use i in your first set of brackets.
(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.')