Security system that allows user to create password [help]

This is the place for queries that don't fit in any of the other categories.

Security system that allows user to create password [help]

Postby ahmedruler1 » Thu Aug 29, 2013 10:42 pm

I am trying to create a security system that creates a username from an inputted first and last name.

For example: I enter "John Smith," and it takes the first 4 characters from the last name and makes it "Smit." It then takes the first letter from first name, making it "smitJ." But, that's not what where I'm stuck.

I'm having trouble where it asks to create a password. The password needs to contain at least 2 numbers, and mustn't be longer than 10 characters. When you enter a password without any numbers, it will ask again for numbers. If it has numbers, but is too short, it will ask for it again. Finally, if you don't enter the password correctly, it will end after 3 tries.

Here's what I have done so far:

Code: Select all
#Program to create security system for a website (Username,password,security pin)

#Declaire/initialize variable
#!/usr/bin/python
sMessage = "" #message for user string
sLastName = "" #(Ask user for last name)
sFirstName = "" #(Ask user for first name)
sPassword = ""  #( Password for user)
sPasswordVerify = "" #(varify if the password is entered the same as before)
sList = ""
sUsername = "" #(creates a username from first and last name input)
sPin= ""#(User creates his own pin for extra security)
iCount = 0 #(counts the amount of)
sMenuChoice = 0

def get_password():
    for password_tries in range(3):
        password = input("Enter your password: ")

        if len(password) < 6:
            print("Your password is too short (minimum of 6 characters).")

        elif len(password) > 10:
            print("Your password is too long (maximum of 10 characters).")

        elif sum(c.isdigit() for c in password) < 2:
            print("Your password does not contain enough digits (minimum 2).")

        else: # none of the previous checks was hit, so password is valid!
            return password

        print("Too many failed password entries, giving up.")
        return None # or maybe raise an exception here, instead           


#Ask for the users First name

while len(sFirstName) <2:
    sFirstName=input("Please Enter your first name: ")
    if len(sFirstName) <2:
        print("ERROR: You must enter a minumum or 2 Characters")

#Ask for the users Last name       
while len(sLastName)  <4:
    sLastName = input("please enter your Last name: ")
    if len(sLastName) <4 :
        print ("Error you must enter your Last name with a minimum of 4 characters")
sMessage = "Your Username is " + sLastName[0:4] + sFirstName[0:1]
print (sMessage)

while not sMenuChoice == "1":
    sMenuChoice = str(input("To Create a password type 'ok' "))

    if sMenuChoice == "ok":
            get_password()
ahmedruler1
 
Posts: 7
Joined: Tue Jun 25, 2013 6:29 am

Re: Security system that allows user to create password [he

Postby StormJedi » Fri Aug 30, 2013 12:47 am

ahmedruler1 wrote:I'm having trouble where it asks to create a password. The password needs to contain at least 2 numbers, and mustn't be longer than 10 characters. When you enter a password without any numbers, it will ask again for numbers. If it has numbers, but is too short, it will ask for it again. Finally, if you don't enter the password correctly, it will end after 3 tries.


Are you getting an error? Or when you input a password, you don't get the output you were expecting?
EDIT: I ran the program and noticed that if you do not input a password that is within the proper variables, the loop exits instantly.

EDIT2:
Code: Select all
def get_password():
    for password_tries in range(3):
        password = input("Enter your password: ")

        if len(password) < 6:
            print("Your password is too short (minimum of 6 characters).")

        elif len(password) > 10:
            print("Your password is too long (maximum of 10 characters).")

        elif sum(c.isdigit() for c in password) < 2:
            print("Your password does not contain enough digits (minimum 2).")

        else: # none of the previous checks was hit, so password is valid!
            return password

        print("Too many failed password entries, giving up.")
        return None # or maybe raise an exception here, instead 

Well, I think I found your problem, the print and return none are in the loop, and not being in the if statement, are always run.
I moved the print and return none outside of the for loop, and then I nested the if statements.

Code: Select all
def get_password():
   for password_tries in range(3):
      password = input("Enter your password: ")
      
      if len(password) > 6:
         
         if len(password) < 10:
            
            if sum(c.isdigit() for c in password) >= 2:
               
               return password
               break
               
            else:
               print("Your password does not contain enough digits (minimum 2).")
         else:
            print("Your password is too long (maximum of 10 characters).")
      else:
         print("Your password is too short (minimum of 6 characters).")   
   print("Too many failed password entries, giving up.")
   return None # or maybe raise an exception here, instead

Notice how I changed the signs, now for the first if, if the length of the password is GREATER than 6, the program continues.
Next if, if the password is LESS than 10, the program continues.
Final if, if the password has TWO OR MORE digits, the password is returned, and the loop breaks.

Should any of these criteria not be met, the next if is never tested, the program simply loops from the beginning.

However, upon a successful password choice, the menu simply loops again, I assume you are still working on the menu.
If you do a statement like this:
Code: Select all
print (get_password())

You get the password, assuming it meets all the criteria.

NOTE: This is probably not the most efficient way to accomplish this, but it is the way I would do it.
Attachments
test.JPG
test.JPG (25.03 KiB) Viewed 216 times
Python 2.7
Python 3.3
Windows 7 x64
Ubuntu 12.10 x64
One day I hope to use one OS and 1 Python flavor, until that day, life goes on.
StormJedi
 
Posts: 27
Joined: Fri Feb 22, 2013 3:36 pm

Re: Security system that allows user to create password [he

Postby ochichinyezaboombwa » Fri Aug 30, 2013 8:18 pm

Although this code needs some major rework, to make it do what you want all you need to do is unindent two lines:
Code: Select all
    print("Too many failed password entries, giving up.")
    return None # or maybe raise an exception here, instead 
instead of
Code: Select all
        print("Too many failed password entries, giving up.")
        return None # or maybe raise an exception here, instead 
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 6 guests