Password Saver Project - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Password Saver Project (/thread-5561.html) Pages:
1
2
|
Password Saver Project - jhenry - Oct-10-2017 I'm having trouble with a project that I have to create a password saver for. I'm running into an indent error that I have no clue on how to fix. The error I get is at line 117 return unencryptedMessage ^ IndentationError: unexpected indent my code is here: 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. 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 == '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)): if passwordToLookup in passwords [0]: print (password) #The encryption key for the caesar cypher unencryptionKey=-16 #Caesar Cypher Encryption def passwordunEncrypt (encryptedMessage, key): #We will start with an empty string as our encryptedMessage unencryptedMessage = '' #For each symbol in the unencryptedMessage we will add an encrypted symbol into the encryptedMessage for symbol in encryptedMessage: 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 unencryptedMessage += chr(num) else: unencryptedMessage += symbol return unencryptedMessage Here is the line I get the error at 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) ####### YOUR CODE HERE ###### #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 # You can consult the reading on lists in Week 5 for ways to loop through a list # #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. # #3. If the name is found then decrypt it. Decrypting is that exact reverse operation from encrypting. Take a look at the # caesar cypher lecture as a reference. You do not need to write your own decryption function, you can reuse passwordEncrypt # # Write the above one step at a time. By this I mean, write step 1... but in your loop print out every item in the list # for testing purposes. Then write step 2, and print out the password but not decrypted. Then write step 3. This way # you can test easily along the way. # ####### 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 ###### #You will need to encrypt the password and store it in the list of passwords #The encryption function is already written for you #Step 1: You can say encryptedPassword = passwordEncrypt(unencryptedPassword,encryptionKey)] #the encryptionKey variable is defined already as 16, don't change this #Step 2: create a list of size 2, first item the website name and the second item the password. #Step 3: append the list from Step 2 to the password list ####### 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() if(choice == '7'): print ("What website password would you like to delete?") for keyvalue in passwords: print(keyvalue[0]) passwordToDelete = input() for i in range (len(passwords)): if passwordToDelete in passwords [i][0]: passwords.remove print() print()[/i] RE: Password Saver Project - sparkz_alot - Oct-10-2017 It is difficult to tell since your post itself contains no indentation and is way to long to try and fix manually. RE: Password Saver Project - jhenry - Oct-10-2017 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. 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 == '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)): if passwordToLookup in passwords [i][0]: print (password) #The encryption key for the caesar cypher unencryptionKey=-16 #Caesar Cypher Encryption def passwordunEncrypt (encryptedMessage, key): #We will start with an empty string as our encryptedMessage unencryptedMessage = '' #For each symbol in the unencryptedMessage we will add an encrypted symbol into the encryptedMessage for symbol in encryptedMessage: 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 encrypted = passwordEncrypt("Hello world!", 16) passwordEncrypt(encrypted, -16) print(passwordunEncrypt(passwords[i][1], 16)) ####### YOUR CODE HERE ###### #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 # You can consult the reading on lists in Week 5 for ways to loop through a list # #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. # #3. If the name is found then decrypt it. Decrypting is that exact reverse operation from encrypting. Take a look at the # caesar cypher lecture as a reference. You do not need to write your own decryption function, you can reuse passwordEncrypt # # Write the above one step at a time. By this I mean, write step 1... but in your loop print out every item in the list # for testing purposes. Then write step 2, and print out the password but not decrypted. Then write step 3. This way # you can test easily along the way. # ####### YOUR CODE HERE ###### if(choice == '3'): print("What website is this password for?") website = input() print("What is the password?") unencryptedPassword = input() encryptedPassword = passwordEncrypt newsitepassword = [website, encryptedPassword] passwords.append (newsitepassword) ####### YOUR CODE HERE ###### #You will need to encrypt the password and store it in the list of passwords #The encryption function is already written for you #Step 1: You can say encryptedPassword = passwordEncrypt(unencryptedPassword,encryptionKey)] #the encryptionKey variable is defined already as 16, don't change this #Step 2: create a list of size 2, first item the website name and the second item the password. #Step 3: append the list from Step 2 to the password list ####### 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() if(choice == '7'): print ("What website password would you like to delete?") for keyvalue in passwords: print(keyvalue[0]) passwordToDelete = input() for i in range (len(passwords)): if passwordToDelete in passwords [i][0]: passwords.remove print() print()
RE: Password Saver Project - sparkz_alot - Oct-10-2017 Without reading the entire code, I would say you need to unindent line 90, then unindent line 117 1 level. In fact, check all indentation after line 90. RE: Password Saver Project - gruntfutuk - Oct-11-2017 There are several places where you have clearly gone wrong with your indentation, so code is not within the code blocks you intended in several cases. This is where pen & paper, where you can clearly mark out the blocks makes things easier. If you are using an IDE/text-editor to write your code that supports folding, you can use that to help identify the blocks as well. RE: Password Saver Project - jhenry - Oct-11-2017 I don't know proper indentation any resources that can help me out. I'm using pycharm so ill look into that RE: Password Saver Project - gruntfutuk - Oct-11-2017 Indenting is fundamental and you really have to get your head around it. I know it seems confusing at first, but after a while it will be obvious. Many other languages use strict formatting and/or the use of lots of brackets {} to help the computer understand how different statements in code relate to each other. At its simplest, any statement used to start a "block" of code has a colon at the end and then everything that should happen in relation to that start point is indented by a certain number of spaces or tabs. Some people (places) prefer to use spaces, some tabs. You need to be consistent. Do not mix them (you can set your editor, in most cases, to convert tabs to spaces). Most commonly, people use either 2 spaces (2 space tabs) or 4 spaces (4 space tabs). Be consistent, always use same spacing size. If within an indented block of code, there is a statement (sub-statement if your prefer) that also requires some code to be run subsidiary to that statement, then that to will end with a colon and the lines underneath will be further indented. Here's a simple example, where a every number in a specified range is tested to see if it is a prime number (note that this is not the most efficient approach to that problem). num = range(2, 20) # change range to check as desired for n in num: # check each n in range to see if is prime for x in range(2, n): # divide each n(umber) by all lower numbers from 2 up if n % x == 0: # if modulo n/x is 0, no remainder, then not prime print("{:4d} is NOT a prime number".format(n)) break # no point looking at higher values of x else: # nobreak - i.e. what to do if iter completed without match print("{:4d} is a prime number".format(n)) # didn't break, must be prime print("All done")Note in particular the else: line, which refers to the end of the inner for loop condition rather than being part of the if condition that it would belong with if it was indented another four spaces. Here's a useful video illustrating the above. RE: Password Saver Project - buran - Oct-11-2017 (Oct-11-2017, 10:26 AM)gruntfutuk Wrote: Some people (places) prefer to use spaces, some tabs. You need to be consistent. Do not mix them (you can set you editor to convert tabs to spaces). Some people use 2 spaces (2 space tabs) and some 4 spaces (4 space tabs). No doubt some people do 1, or 5, etc. PEP8 is pretty clear: Tabs or Spaces? - Spaces are the preferred indentation method.Tabs should be used solely to remain consistent with code that is already indented with tabs. Indentation - Use 4 spaces per indentation level. So, best would be to comply with the PEP... RE: Password Saver Project - gruntfutuk - Oct-11-2017 (Oct-11-2017, 10:33 AM)buran Wrote:I agree completely, but left out the advice in the comment because:(Oct-11-2017, 10:26 AM)gruntfutuk Wrote: Some people (places) prefer to use spaces, some tabs. You need to be consistent. Do not mix them (you can set you editor to convert tabs to spaces). Some people use 2 spaces (2 space tabs) and some 4 spaces (4 space tabs). No doubt some people do 1, or 5, etc.
RE: Password Saver Project - sparkz_alot - Oct-11-2017 (Oct-11-2017, 09:55 AM)jhenry Wrote: I'm using pycharm so ill look into that Pycharm will automatically check your indentation, check the right side of the editor screen for the red and yellow 'ticks'. It will also automatically insert an indent where appropriate and, unless you changed the default settings, it will automatically convert tabs to 4 spaces. Finally, under the "Code" menu is the option for "Code Cleanup". |