Python Forum
Searching for items within a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Searching for items within a list
#1
For a project for a coding class I'm taking, we've been told to complete the code for an encrypted password saver. For part of this, I need to search for a password within a list of passwords based on the website associated with the password, then print the password. The passwords are saved in the list as follows:

passwords = [["yahoo","XqffoZeo"],["google","CoIushujSetu"]]
Essentially, it's a list filled with lists, organized [website, password].

Originally, I set out to do this via linear search. This is the code I'm using for that:

def linsearch(myItem, myList):
   found = False
   position = 0
   while position < len(myList) and not found:
       if myList[position] == myItem:
           found = True
       position = position +1
   return found
The problem being that whenever I call linsearch, it always returns "false." I assume this is because I'm searching an encrypted list. How could I decrypt the list, search it, print the password, and then encrypt it again? I'm having a hard time figuring it out.

The full code is below. Anything in between "YOUR CODE HERE" or "MY CODE" comments is my own code, the rest was provided by the instructor.

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

#load password from file
def loadPasswordFile(fileName):

   with open(fileName, newline='') as csvfile:
       passwordreader = csv.reader(csvfile)
       passwordList = list(passwordreader)

   return passwordList

#save password to file
def savePasswordFile(passwordList, fileName):

   with open(fileName, 'w+', newline='') as csvfile:
       passwordwriter = csv.writer(csvfile)
       passwordwriter.writerows(passwordList)

# The below section of code is a function that performs a linear search when called
##### my code #####
def linsearch(myItem, myList):
   found = False
   position = 0
   while position < len(myList) and not found:
       if myList[position] == myItem:
           found = True
       position = position +1
   return found
##### my code #####

#menu
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("Please enter a number (1-4)")
   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()

       ####### YOUR CODE HERE ######
       print(linsearch(int(passwordToLookup), passwords))
       ####### YOUR CODE HERE ######


   if(choice == '3'):
       print("What website is this password for?")
       website = input()
       print("What is the password?")
       unencryptedPassword = input()

       ####### YOUR CODE HERE ######
       encryptedPassword = passwordEncrypt(unencryptedPassword, encryptionKey)
       newPassword = [website, encryptedPassword]
       passwords.append(newPassword)
       ####### YOUR CODE HERE ######

   if(choice == '4'): #Save the passwords to a file
           savePasswordFile(passwords,passwordFileName)


   if(choice == '5'): #print out the password list
       for keyvalue in passwords:
           print(', '.join(keyvalue))

   if(choice == '6'):  #quit our program
       sys.exit()

   print()
   print()
Reply


Messages In This Thread
Searching for items within a list - by Yobby - Jun-20-2017, 10:01 PM
RE: Searching for items within a list - by Yobby - Jun-21-2017, 12:13 AM
RE: Searching for items within a list - by DeaD_EyE - Jun-20-2017, 11:07 PM
RE: Searching for items within a list - by Yobby - Jun-21-2017, 01:00 AM
RE: Searching for items within a list - by DeaD_EyE - Jun-21-2017, 01:03 AM
RE: Searching for items within a list - by Yobby - Jun-21-2017, 01:40 AM
RE: Searching for items within a list - by nilamo - Jul-03-2017, 05:53 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to parse and group hierarchical list items from an unindented string in Python? ann23fr 0 244 Mar-27-2024, 01:16 PM
Last Post: ann23fr
  Why do I have to repeat items in list slices in order to make this work? Pythonica 7 1,432 May-22-2023, 10:39 PM
Last Post: ICanIBB
  Finding combinations of list of items (30 or so) LynnS 1 918 Jan-25-2023, 02:57 PM
Last Post: deanhystad
  For Word, Count in List (Counts.Items()) new_coder_231013 6 2,706 Jul-21-2022, 02:51 PM
Last Post: new_coder_231013
  How to get list of exactly 10 items? Mark17 1 2,600 May-26-2022, 01:37 PM
Last Post: Mark17
  how to assign items from a list to a dictionary CompleteNewb 3 1,649 Mar-19-2022, 01:25 AM
Last Post: deanhystad
  Searching in the list quest 4 1,594 Mar-02-2022, 10:30 AM
Last Post: quest
  Reading list items without brackets and quotes jesse68 6 4,756 Jan-14-2022, 07:07 PM
Last Post: jesse68
Question How to gather specific second-level items from a list chatguy 2 1,595 Dec-17-2021, 05:05 PM
Last Post: chatguy
  deleting select items from a list Skaperen 13 4,698 Oct-11-2021, 01:02 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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