Weak Password: Write a program to obtain user details in CSV format. If the password entered by the user satisfies the following constraints, print the user details, else raise an exception “Your password is weak”.
A password is said to be strong if it satisfies the following criteria
i) It should be minimum of 10 characters and a maximum of 20 characters
ii) It should contain at least one digit
iii)It should contain at least one special character (non-numeric, non-alphabetic)
Create a class User with the following attribute,
Attributes | Datatype |
name | str |
mobile | int |
username | str |
password | str |
Use the __init__() method to initialize the attributes.
Write a method called display() inside the class User to display the details of the user in the given format.
Function Name | Function Definition |
display() | display the ‘name’, ‘mobile’, ‘username’ and ‘password’ inside this function. |
Create a driver program called Main.py. In the Main method, obtain inputs from the user. Validate the password and if there is an exception, handle the exception and print “Your password is weak”, else call the display() method in the user class.
Refer sample Input/Output format specification
[All text in bold corresponds to the input and rest corresponds to the output]
Sample Input and Output 1:
Enter the user details
John Doe,9876543210,john,johndoe
Your password is weak
Sample Input and Output 2:
Enter the user details
Jane doe,9876543210,Jane,Janedoe@123
Name : Jane doe
Mobile : 9876543210
Username : Jane
Password : Janedoe@123
Solution
class User:
def __init__(self, name, mobile, username, password):
self.name = name
self.mobile = mobile
self.username = username
self.password = password
def display(self):
print("Name : {}".format(self.name))
print("Mobile : {}".format(self.mobile))
print("Username : {}".format(self.username))
print("Password : {}".format(self.password))
# sauravhathi
print("Enter the user details")
name, mobile, username, password = raw_input().split(",")
try:
if len(password) >= 10 and len(password) <= 20 and any(i.isdigit() for i in password) and any(i.isalpha() for i in password) and any(i.isalnum() for i in password):
user = User(name, mobile, username, password)
user.display()
else:
raise Exception("Your password is weak")
except Exception as e:
print(e)
Happy Learning – If you require any further information, feel free to contact me.