I\'m new to learning Python 3.6, and have been trying some practice problems. I\
ID: 3809822 • Letter: I
Question
I'm new to learning Python 3.6, and have been trying some practice problems. I'm having some trouble with this one, from the instructions given, and would really appreciate some help, especially with the code layout, indentation... any additional information, like as explaination of each step in the code would also be really helpful and appreciated.
__________________________________________________________________________________________
Write a program for a user accounts management system, using Python dictionary structure
The program should fulfill the following requirements:
• All the username and password should be kept in a dictionary. In the dictionary, please use username as key and password as the value for each item.
• First the program tells the user “enter 1 to register, enter 2 to sign in, enter 3 to quit”
• After the user enter 1, the program ask the user to input username and password, the username can not be the same as any existing ones. The username must contain at least 4 characters. If the entered username does not satisfy these two conditions, ask the user for a new username
• During the registration, once the user entered a valid username, ask the user to create a password. The password has to contain at least one letter and one digit. The length of the password should be more than 4 characters. If the password provided by the user does not satisfy the requirements, ask the user to create a new password.
• After registration, tell the user “Congratulations! You have setup a new account” and direct the user back to the starting point.
• If the user enters 2, tell the user either “Wrong password” or “Wrong username” until the user enters a correct username and password combination
• The program counts the number of attempts made by the user in entering password/username. If the user tried more than 4 times for either password or username without success, indicate to the user "Exceeded username attempts, please contact the system administrator" or "Exceeded password attempts, please contact the system administrator" Then exit the program (you can use exit() statement). The counts for username and password attempts should be kept separately.
• Display “Welcome back!” after user entered the correct user name and password.
.....................................................................................................
There was also a .Py file provided as the "Password Check Function," to use in the program, for checking password validity. This is what it contained:
def check_password (psswd):
containAlpha = any(c.isalpha() for c in psswd) #Checks if a password contains any letter, or any digit and has length bigger than 4
containNum = any(c.isdigit() for c in psswd) #Checks if the password contains at least one letter
return (containAlpha and containNum and len(psswd)>4) #Checks if the password contains at least one digit
print(check_password("abcdef"))
print(check_password("123456"))
print(check_password("ab12"))
print(check_password("abc123"))
Explanation / Answer
# link for code in case indentation mess up: https://repl.it/GrU7/1
users = {}
def validateUser(user):
if user in users:
print("User already exist try again.")
return False
if len(user) < 4:
print("User name should contain atleast 4 characters. Try again")
return False
return True
def check_password (psswd):
containAlpha = any(c.isalpha() for c in psswd) #Checks if a password contains any letter, or any digit and has length bigger than 4
containNum = any(c.isdigit() for c in psswd) #Checks if the password contains at least one letter
return (containAlpha and containNum and len(psswd)>4) #Checks if the password contains at least one digit
while True:
choice = int(input("Enter 1 to regitser, enter 2 to sign n, enter 3 to quit: "))
if choice == 1:
while True:
user = input("Enter username: ")
if not validateUser(user):
continue
else:
break
while True:
password = input("Enter password: ")
if not check_password(password):
print("Invalid password. Try again")
continue
else:
break
print("Congratulations! You have setup a new account")
users[user] = password
continue
elif choice == 2:
attempts = 0
while True:
user = input("Enter username: ")
attempts += 1
if not user in users:
print("Wrong username")
if attempts >= 4:
print("Exceeded username attempts, please contact the system administrator")
exit(1)
else:
print("Try again")
continue
password = input("Enter password: ")
if password != users[user]:
print("Wrong password")
if attempts >= 4:
print("Exceeded password attempts, please contact the system administrator")
exit(1)
else:
print("Try again")
continue
break
print("Welcome back!")
elif choice == 3:
break
else:
print("Please choose form given menu")
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.