python assignment problems

Discuss how to write good code, break bad code, your current pet projects, or the best way to approach novel problems

python assignment problems

Post by ghostheadx2 on Fri Mar 10, 2017 4:01 am
([msg=93510]see python assignment problems[/msg])

So here's the problem. I have had two assignments in my CIT270 class. I have had similar issues on both assignments. The thing is, they are the same assignment in two different languages: one in Perl and one in Python. I come close to making it work, but I get similar kinds of errors for each.

Assignment instructions in Python:

Code: Select all
CIT 270L Integrative Programming
Assignment #2—Managing a User List (Revisited)
Due 3/9 in Lab section / Moodle
Objective:
To manage a user list that can be modified and saved to a text file.
Inputs:


Output:


Input text file consisting of pairs of usernames and encrypted passwords, separated by a colon (:) without any
spaces
User choice: (‘add’-add user account, ‘edit’-edit existing user account, ‘delete’- delete existing user account,
‘print’- print list of user accounts, ‘quit’-save and quit)
List of user accounts, error messages when appropriate
Output text file consisting of pairs of username and encrypted passwords, separated by a colon (:) without any
spaces
Specification:
 The program maintains a list of user accounts and passwords supplied by a system administrator.
 Any inputted username should be stripped of any non-alphanumeric characters (special characters such as ! @
# $ % ^ & * ( ) _ + ; : ’ ”) using: username = re.sub(r'\W+','',username)
 In order to use the re.sub command, you need to import the appropriate library with "import re"
 To convert the username to lowercase, use: username = username.lower()
 Any inputted password should be stripped of the apostrophe character ' using:
password = password.replace('\'', '') and encrypted by reversing the text (note: unlike Perl,
there is no built-in function to reverse a string, a user-defined function must be built):
password = reverse(password)
 Each option should be implemented as a separate function within a Module that will be imported, so that its
functions can be called. Valid options will be stored in a dictionary to test for invalid options:
function = {‘add’:add_user, ‘edit’:edit_user, ‘delete’:delete_user,
‘print’:print_list}
which can be called by assigning a variable to the key of the dictionary (hash), such as:
selection = function[choice]
and updating the dictionary as: hash = selection(hash)
The usernames and passwords will be loaded from an input file and stored in a dictionary. (Be sure to strip off the
newline from the password: using the method strip(“\n”). The dictionary will be passed as a parameter
from the Python script file to the appropriate function in the module file and updated according to the function.
The script file should import the functions from the module file to call the appropriate function.
Initially, the program prompts for an input text file to read from. If the file does not exist, it should be created.
The program should then loop until the user chooses to quit (selects status as ‘q’). If the user enters an illegal
status, the program will prompt again for the status input. Upon quitting, the program prompts the user to save
the list to an output file with the same name as the original input text file.
What to turn in:
A single zipped file (asmt2_yourlastname.zip) containing both source code files (the Python script called:
asmt2_yourlastname.py, and the Python module file called: functs_yourlastname.py) submitted via Canvas
(http://canvas.csun.edu) to the Lab section (not the lecture section). Any deviation from the format for submission
will result in an automatic -10%.


Here is what the output of the program should look like:

Code: Select all
Sample Output:
% more myfile.txt
einstein:2^cm=e
newton:am=f
%python asmt2_mcilhenny.py
Enter file to open: myfile.txt
User accounts
-------------
add = Add user account
edit = Edit existing user account
delete = Delete existing user account
print = Print list of user accounts
quit = Save and quit
Enter choice: add
Enter username: pythagoras
Enter password: a^2+b^2=c^2
User accounts
-------------
add = Add user account
edit = Edit existing user account
delete = Delete existing user account
print = Print list of user accounts
quit = Save and quit
Enter choice: add
Enter username: einstein
Username already exists!
User accounts
-------------
add = Add user account
edit = Edit existing user account
delete = Delete existing user account
print = Print list of user accounts
quit = Save and quit
Enter choice: edit
Enter username to edit: fibonacci
Username does not exist!
User accounts
-------------
add = Add user account
edit = Edit existing user account
delete = Delete existing user account
print = Print list of user accounts
quit = Save and quit
Enter choice: edit
Enter username to edit: newton
Enter current password: f=m*a
Incorrect password!User accounts
-------------
add = Add user account
edit = Edit existing user account
delete = Delete existing user account
print = Print list of user accounts
quit = Save and quit
Enter
Enter
Enter
Enter
choice: edit
username to edit: newton
current password: f=ma
new password: force=mass*acceleration
User accounts
-------------
add = Add user account
edit = Edit existing user account
delete = Delete existing user account
print = Print list of user accounts
quit = Save and quit
Enter choice: delete
Enter username to delete: pythagoras
Enter password: I_forgot_my_password
Incorrect password!
User accounts
-------------
add = Add user account
edit = Edit existing user account
delete = Delete existing user account
print = Print list of user accounts
quit = Save and quit
Enter choice: delete
Enter username to delete: pythagoras
Enter password: a^2+b^2=c^2
User deleted
User accounts
-------------
add = Add user account
edit = Edit existing user account
delete = Delete existing user account
print = Print list of user accounts
quit = Save and quit
Enter choice: print
einstein:2^cm=e
newton:noitarelecca*ssam=ecrof
User accounts
-------------
add = Add user account
edit = Edit existing user account
delete = Delete existing user account
print = Print list of user accounts
quit = Save and quit
Enter choice: quit
Save contents? (y/n): y
% more myfile.txt
einstein:2^cm=e
newton:noitarelecca*ssam=ecrof


Here is my python code:

Code: Select all
import functs_koloff

#create a dictionary of functions
function = ['add', 'edit', 'delete', 'print', 'quit']

# create a variable called dictionary
#global var declarations go here
dictionary={}

# add prompt for the filename from the r, save into fh

#Open file for reading

#take raw_input from file
fn = raw_input("Enter file name to open: ")

#if file does not exist, create it
fh = open(fn, "a+")

   #copy all lines into list at one time, readlines into list

for line in fh:
   line_array = line.split(":")
   r = line_array[0]
   pwd = line_array[1].strip("\n")
   dictionary[user] = pwd

while(True):
   #loop to select options
   print("add = ")
   print("edit = ")
   print("delete = ")
   print("print = ")
   print("quit = ")
   print("Enter choice: ")
   choice = raw_input()
   #determine if raw_input is to quit, then go to next step of program
#      if (choice == 'q'):
#         break

   if ( choice in function):
      if (choice == "add"):
         hash= functs_koloff.add_user(hash)
      elif (choice == "delete"):
         hash= functs_koloff.delete_user(hash)
      elif (choice == "edit"):
         hash= functs_koloff.edit_user(hash)
      elif (choice == "print"):
         hash= functs_koloff.print_list(hash)
      elif (choice == "quit"):
         saveit= input ("Do you want to Save Changes? : y/n :")
         if (saveit == 'y'):
            fh = open (fn, 'w')
            for  user, pwd in hash.items():
               fh.write(user+":"+pwd+"\n")
            fh.close()
            sys.exit()
      else:
         print ("That is not an option."); continue      
      #loop
   #   elif choice not in function: print "that is not an option"; continue
   #call appropriate function by using option
#      else:
#         selection = function[choice]
#         dictionary = selection(dictionary)

   #go to next step of program after options menu
   #this stuff below needs to be rewritten in python as well

#close file writing one and then prompt to save it
saveit= raw_input("Do you want to Save Changes? : y/n :")
if (saveit == 'y'):
   fh = open (fn, 'w')
   for  user, pwd in hash.items():
      fh.write(user+":"+pwd+"\n")
   fh.close()


If anyone can hint me in the right direction, that would be great. I've revised it several times. Once I finish this, all I have to do is manually translate it into ruby using my brain. So by helping me with this assignment, you are indirectly helping me with two assignments. Could someone please look at my code and give me some input as to what I'm doing wrong? Thanks.
ghostheadx2
Contributor
Contributor
 
Posts: 726
Joined: Wed Nov 19, 2014 1:19 am
Blog: View Blog (0)


Re: python assignment problems

Post by pretentious on Sun Mar 12, 2017 9:13 pm
([msg=93515]see Re: python assignment problems[/msg])

I have never heard of the functs_koloff module and google gives me nothing. Is it a custom one that you created?
Goatboy wrote:Oh, that's simple. All you need to do is dedicate many years of your life to studying security.

IF you feel like exchanging ASCII arrays, let me know ;)
Can you say brainwashing It's a non stop disco
User avatar
pretentious
Addict
Addict
 
Posts: 1153
Joined: Wed Mar 03, 2010 12:48 am
Blog: View Blog (0)


Re: python assignment problems

Post by ghostheadx2 on Wed Apr 19, 2017 5:24 pm
([msg=93616]see Re: python assignment problems[/msg])

Yes. I passed the assignment though so it's all good.
ghostheadx2
Contributor
Contributor
 
Posts: 726
Joined: Wed Nov 19, 2014 1:19 am
Blog: View Blog (0)



Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests

cron