2017-02-27 92 views
1

我们编码的密码保护程序,我被困在嵌套列表循环中。创建一个循环,通过可以追加的嵌套列表

passwords = [["yahoo","XqffoZeo"],["google","CoIushujSetu"]] 

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() 

这是我到目前为止。我现在需要创建一个给出密码的循环,并且如果密码列表附加了更多网站及其密码,则该密码将起作用。

下面是整个代码:由于这是一项任务,而不是个人项目,因此我一次一小部分地完成了它。

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 enc sage 
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. 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() 

    ####### THIS IS WHERE I'm WORKING #### 
#NOt supposed to change any of the base code just working on this part currently. 

    for list in passwords: 


    #Perameters: You will need to find the password that matches the website 
    #You will then need to decrypt the password 

    #1. Create a loop that goes through each item in the password list 

    #2. Check if the name is found. To index a list of lists you use 2 square bracket sets 

    #3. If the name is found then decrypt it. 





print() 
print() 
+1

? – chepner

+0

你想达到什么目的? –

+0

因为我遵循教授给出的参数。我们不应该更改基本代码以使其更清洁,因为我们“尚未覆盖该材料”。 – Inky

回答

0

可以解开内部列表分为两个不同的变量,而不是一个,是这样的:你为什么要使用嵌套列表,而不是`dict`

passwords = [["yahoo","XqffoZeo"],["google","CoIushujSetu"]] 

if(choice == '2'): #Lookup at password 
    print("Which website do you want to lookup the password for?") 
    for key, _ in passwords: 
     print(key) 
    passwordToLookup = input().strip() 
    for key, value in passwords: 
     if key == passwordToLookup: 
      print(value) 
+0

我不确定OP要打印密码... –