May-01-2020, 01:54 PM
Hello all,
I have figured out the main parts of the assignment and am just trying to add the extra credit piece (add a 'delete password' function). I have done so and it works... but only if following these steps:
Program loads > select option 4 > delete a password > select 2 > check password > select 3 > add a password > select 2 or 4 > repeat or end
If I choose anything else before option 4 or if I add two passwords in a row and then try to delete a password, it deletes two passwords instead. Can you review my code and see where I went wrong? Not looking for an answer, just looking for some guidance. The only way I even got the program to delete passwords last night was from the line passwordToRemove = [i for i in websiteChoice if i in passwords]. I've tried to use del, remove, and pop functions and the program either didn't do anything or gave me ValueError: list.remove(x): x not in list error.
I am sure this is probably glaringly easy for all of you! Sorry, I am new to this!
I have figured out the main parts of the assignment and am just trying to add the extra credit piece (add a 'delete password' function). I have done so and it works... but only if following these steps:
Program loads > select option 4 > delete a password > select 2 > check password > select 3 > add a password > select 2 or 4 > repeat or end
If I choose anything else before option 4 or if I add two passwords in a row and then try to delete a password, it deletes two passwords instead. Can you review my code and see where I went wrong? Not looking for an answer, just looking for some guidance. The only way I even got the program to delete passwords last night was from the line passwordToRemove = [i for i in websiteChoice if i in passwords]. I've tried to use del, remove, and pop functions and the program either didn't do anything or gave me ValueError: list.remove(x): x not in list error.
I am sure this is probably glaringly easy for all of you! Sorry, I am new to this!
import csv import sys #The password list - We start with it populated for testing purposes passwords = [["yahoo","XqffoZeo"],["google","CoIushujSetu"]] #The password file name to store the passwords to passwordFileName = "samplePasswordFile" #The encryption key for the caesar cypher encryptionKey=16 #Caesar Cypher Encryption def passwordEncrypt (unencryptedMessage, key): #We will start with an empty string as our encryptedMessage encryptedMessage = '' #For each symbol in the unencryptedMessage we will add an encrypted symbol into the encryptedMessage for symbol in unencryptedMessage: if symbol.isalpha(): num = ord(symbol) num += key if symbol.isupper(): if num > ord('Z'): num -= 26 elif num < ord('A'): num += 26 elif symbol.islower(): if num > ord('z'): num -= 26 elif num < ord('a'): num += 26 encryptedMessage += chr(num) else: encryptedMessage += symbol return encryptedMessage def loadPasswordFile(fileName): with open(fileName, newline='') as csvfile: passwordreader = csv.reader(csvfile) passwordList = list(passwordreader) return passwordList def savePasswordFile(passwordList, fileName): with open(fileName, 'w+', newline='') as csvfile: passwordwriter = csv.writer(csvfile) passwordwriter.writerows(passwordList) 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. Delete a password") print(" 5. Save password file") print(" 6. Print the encrypted password list (for testing)") print(" 7. Quit program") print("Please enter a number (1-7)") choice = input() if(choice == '1'): #Load the password list from a file passwords = loadPasswordFile(passwordFileName) 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() for i in range(len(passwords)): #step 1 - loops through the list of passwords if passwordToLookup == passwords[i][0]: #step 2 - checks if the name is found print(passwordEncrypt(passwords[i][1], -16)) #step 3 - prints out the passwords through encryption if(choice == '3'): print("What website is this password for?") website = input() print("What is the password?") unencryptedPassword = input() encryptedPassword = passwordEncrypt(unencryptedPassword, 16) #step 1 - encrypts the new password newPassword = [website, encryptedPassword] #step 2 - creates a list including the website name and encrypted password passwords.append(newPassword) #step 3 - adds the new password to the list if (choice == '4'): #delete a password print('Which website password do you wish to remove? ') for keyvalue in passwords: print(keyvalue[0]) websiteChoice = input() passwordToRemove = [i for i in websiteChoice if i in passwords] for passwordToRemove in (passwords): passwords.remove(passwordToRemove) print('Your password for ' + str(websiteChoice) + ' has been successfully removed.') if(choice == '5'): #Save the passwords to a file savePasswordFile(passwords,passwordFileName) if(choice == '6'): #print out the password list for keyvalue in passwords: print(', '.join(keyvalue)) if(choice == '7'): #quit our program sys.exit() print() print()