Python Forum
Python Password Saver Assignment
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Password Saver Assignment
#1
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!

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()
Reply
#2
Your delete logic is backward. You want to delete each item that is in the passwordToRemove list. Instead you deleted all the passwords. The only reason your password list wasn't completely empty is because removing items from a list over which your are iterating produces strange results. You want to do this:
        for password in passwordToRemove :
            passwords.remove(password)
        or
        passwords.remove(passwordToRemove[0])
Why didn't you use a dictionary to store the passwords. You wrote a lot of code to look things up in you list that you get for free using a dictionary. For example:
        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
becomes
    entry = input('Enter website: ')
    if entry in passwords:
        print(encrypt(passwords[entry], -encryption_key))
    else:
        print('Enty did not match any website')
And deleting an entry goes from:
        websiteChoice = input()
        passwordToRemove = [i for i in websiteChoice if i in passwords]
        passwords.remove(passwordToRemove[0])
        print('Your password for ' + str(websiteChoice) + ' has been successfully removed.')
to
    entry = input('Enter website: ')
    if entry in passwords:
        del passwords[entry]
    else:
        print('Enty did not match any website')
Reply
#3
Hey,

Thank you for the information! We did not learn about dictionaries specifically and the course relied heavily on for loops. Thank you for explaining an easier way, I figured there was one!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python for Everybody 5.2 assignment baba04201 20 173,153 Jul-25-2023, 04:15 PM
Last Post: MicaelSchutz
  Python for Everybody 3.1 assignment ramadan2099 18 45,332 Jan-23-2021, 06:27 AM
Last Post: KonohaHokage
  Password Checker Assignment mcostello 1 5,054 Nov-14-2020, 07:54 AM
Last Post: DPaul
  Coursera python for everybody 5.2 assignment SteppentigerV2 11 12,787 Oct-22-2020, 11:57 AM
Last Post: Larz60+
  Python project help (Password manger using mysql) lifeofpy 2 4,638 Jul-31-2020, 06:18 PM
Last Post: deanhystad
  [split] Python for Everybody 5.2 assignment ramadan2099 3 12,026 Jul-15-2020, 04:54 PM
Last Post: Bipasha
  Python Assignment 3 - Applied Data Science - 2.3 eyavuz21 8 4,927 Jun-06-2020, 08:59 AM
Last Post: eyavuz21
  Python for Everybody 3.3 assignment ramadan2099 7 31,713 Apr-08-2020, 06:49 AM
Last Post: DeaD_EyE
  Python for everyone course assignment 5.2 ofekx 3 8,520 Dec-23-2019, 08:41 PM
Last Post: nilamo
  [split] Python for Everybody 5.2 assignment jonchanzw 4 8,435 Oct-22-2019, 08:08 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

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