In: Computer Science
To be done in Python 3.7
Some Web sites impose certain rules for passwords. Write a function that checks whether a string is a valid password. Suppose the password rules are as follows:
A password must have at least eight characters.
A password must consist of only letters and digits.
A password must contain at least two digits.
Write a program that prompts the user to enter a password and displays valid password if the rules are followed or invalid password otherwise.
Your program should define a class called Password, which is in its own file called password.py. You should have another file called assn13-task2.py that has code the creates and uses a Password object. All input and print functions should be in in this file. Your program will prompt the user for a password, and after completing will ask the user if they want to enter another. The program should only ever create one instance of Password. Your Password class should have at least the following:
password.py file:
Code for the above password.py file:
class Password: __message = '' errorMessage = [] def setPassword(self,passString): self.__message = passString return self.isValid() def getErrorMessage(self): temp = self.errorMessage.copy() self.errorMessage = [] return temp def isValid(self): v=0 if self.__message == 'password': self.errorMessage.append('A password cannot contain the word ‘password’') v=1 if len(self.__message)>8: if self.__message[-3:] == '123': self.errorMessage.append('A password cannot end with ‘123’') v=1 else: self.errorMessage.append('A password must have at least eight characters.') v=1 if not self.__message.isalnum(): self.errorMessage.append('A password must consist of only letters and digits.') v=1 if sum(character.isdigit() for character in self.__message) < 2: self.errorMessage.append('A password must contain at least two digits.') v=1 return True if v==0 else self.getErrorMessage()
assn13-task2.py file:
Code for the above assn13-task2.py file:
from Password import Password p = Password() exit = 1 while(exit!=0): print('\nEnter password: ', end="") res = p.setPassword(input().strip()) if res == True: print('Entered String is a Valid Password') else: for i in res: print(i) print('\nDo you want to check another password(1=>yes / 0=>no): ',end=" ") exit = int(input())
Output for running the assn13-task2.py file: