Hal Higgins
Hal Higgins

Reputation: 1

resetting password python program

I need to make a password reset program that makes the user input their new password twice so that the computer knows that the user has not made any mistakes when typing their new password. it should also only accept the password if it is eight characters or less and includes both lowercase and uppercase letters. here is what i have so far:

import os
import time

def main():
    while True:
        PassWord = input ("Enter Password: ")

          for c in s:
              if c.islower():
                print c
          Password == :
            time.sleep(1)
            print ("Login successful!")
            logged()

        else:
            print ("Please try again")

def logged():
    time.sleep(1)
    print ("Welcome to ----")


main()

Upvotes: 0

Views: 13688

Answers (3)

Akash
Akash

Reputation: 110

import time
import re

password=''
password2=''
def validate_password(password):
      if len(password) <8:
            print 'returning 1'
            return 1
      if not re.search('[0-9]',password):
            return 2
      if not re.search('[a-z]',password):
            return 3
      if not re.search('[A-Z]',password):
            return 4
      return 0

def check_password(password,password2):
      if password==password2:
            return True
      else:
            return False

def get_password():
      password=raw_input('Enter password:')
      flag=validate_password(password)
      if flag==0:
            password2=raw_input('Re-Enter Password:')
            flag2=check_password(password,password2)
            if flag2==True:
                  return True
            else:
                  print 'Passwords do not match!!'
                  return False
      elif flag==1:
            print 'Password must be of 8 characters'
            return False
      elif flag==2:
            print 'Password must contain a number'
            return False
      elif flag==3:
            print 'Password must include a small letter'
            return False
      elif flag==4:
            print 'Password must also include a small letter'
            return False
def logged():
      print 'Logging in...'
      time.sleep(5)
      print 'Successfully logged in'

def main():
      flag=get_password()
      if flag==True:
            logged()
      else:
            while True:
##                  print ''
                  get_password()

main()

Upvotes: 0

Luke
Luke

Reputation: 774

I'm not really sure what authentication system allows for eight or less characters, but sure, we can make it work.

When it comes to dealing with passwords, you shouldn't really use plain input. In this case, anyone looking over your shoulder can easily get your password. You should use getpass instead, which is part of the standard library. A function will check if the password meets the conditions and return a boolean value. This isn't the best implementation, but it works for this problem. Hope this helps.

from getpass import getpass


def check_password(password):
    if len(password) > 9:
        return False
    for character in password:
        if character.isupper():
            return True


def main():
    while True:
        password = getpass()
        if password == getpass():
            if check_password(password):
                print('Successfully logged in.')
                break
        else:
            print('Try again!')

if __name__ == '__main__':
    main()

Upvotes: 0

Srini
Srini

Reputation: 1649

There are a lot of syntactical errors in your program. It's worth reconsidering the core logic.

import os
import time
import getpass

def logged():
    time.sleep(1)
    print ("Welcome to ----")


def checklower(pw):
    for c in pw:
        if c.islower():
            return True

def checkupper(pw):
    for c in pw:
        if c.isupper():
            return True

def main():
    while True:  # having this loop because you have it too
        password_1 = getpass.getpass()
        print "One more time"
        password_2 = getpass.getpass()
        if (password_1 != password_2) or not (checkupper(password_1)) or not (checklower(password_2)) or not (len(password_1) >= 8):
            print "Please try again"
            continue
        else:
            logged()
            break

main()

This is a super quick and dirty implementation of what you need

Upvotes: 0

Related Questions