Python Forum
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Assignment
#1
This is an assignment that is due next week and I'm stuck on the for i in range code at the bottom highlighted with purple font.  I have included the instructions from my professor as well as the beginning of the code.  It's a password saver code that we need to make work.

Beginning of code:
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("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()
What I need to make work:
for i in range(len('yahoo, google')):
  print('i')
#1. Create a loop that goes through each item in the password list
#  simplest way to loop through a list is: for i in range(len(NAMEOFLIST))
Reply
#2
look at:

for i = in range(len('yahoo', 'google')):
    print('i')
your instructions were:
Quote:#1. Create a loop that goes through each item in the password list using range
I paraphrased.

The name of your password list is passwordlist ... so, don't you think the range should be:
for i in range(len(passwordlist))
and you should be printing out i, not 'i'

in other words:
for i in range(len(passwordlist)):
    print(i)
Don't try to over think, it's usually (not always) quite simple
Reply
#3
or:
print(passwordlist[i])
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
You might want to also check this post http://python-forum.io/Thread-Beginner-N...76#pid1676
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#5
I still can't get it to work with any of your suggestions, do I have the code in the right spot within the code itself?
Reply
#6
What's your current code, what's the current output, what's the output you're trying to have, and what's the current error (if any)?
Reply
#7
I need for this code

for i in range(len("samplepasswordfile")):
   i = [0,1]

to find these passwords

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

it goes as far as #2, ask me what password I want to look up
yahoo
google

I type in google or yahoo, and it starts all over at #1

within this code

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

       for i in range(len("samplepasswordfile")):
           i = [0,1]
Reply
#8
(Oct-06-2016, 12:58 AM)tinabina22 Wrote:  #1. Create a loop that goes through each item in the password list
#  simplest way to loop through a list is: for i in range(len(NAMEOFLIST))

The simplest way to loop through each item in the password list is
for item in NAMEOFLIST:
    print(item)
As passwords is a list of list you can do the loop like this
passwords = [["yahoo","XqffoZeo"],["google","CoIushujSetu"]]
for site, password in passwords:
    print(site, password)
Output:
yahoo XqffoZeo google CoIushujSetu
In the last post the reason the code starts all over at #1 after inputting a password is because there is no code to tell it to do anything but go back to the while loop.
Reply
#9
(Oct-06-2016, 11:35 PM)Yoriz Wrote:
(Oct-06-2016, 12:58 AM)tinabina22 Wrote: #1. Create a loop that goes through each item in the password list # simplest way to loop through a list is: for i in range(len(NAMEOFLIST))
The simplest way to loop through each item in the password list is
for item in NAMEOFLIST:     print(item)
As passwords is a list of list you can do the loop like this
passwords = [["yahoo","XqffoZeo"],["google","CoIushujSetu"]] for site, password in passwords:     print(site, password)
Output:
yahoo XqffoZeo google CoIushujSetu
In the last post the reason the code starts all over at #1 after inputting a password is because there is no code to tell it to do anything but go back to the while loop.

Thank you very much, finally got it.
Now to move on to the next part of the code I need to finish up on.
Reply


Forum Jump:

User Panel Messages

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