(using python) You have been asked to write a username validation program for a
ID: 3760124 • Letter: #
Question
(using python)
You have been asked to write a username validation program for a small website. The website has specific rules on what constitutes a valid username, including:
All usernames must be between 8 and 15 characters long
Usernames can only contain alphabetic (a-z and A-Z) and numeric characters (0-9) - no special characters are allowed.
The first character in a username cannot be a digit
Usernames must contain at least one uppercase character
Usernames must contain at least one lowercase character
Usernames must contain at least one numeric character
Write a program that asks the user to enter in a username and then examines that username to make sure it complies with the rules above. Here's a sample running of the program - note that you want to keep prompting the user until they supply you with a valid username:
Hint: you will need to count the # of uppercase, lowercase and digit characters using some kind of loop
Explanation / Answer
valid = False;
while(valid == False):
username = input ("Please enter a username:")
length = len(username);
if(length < 8 or length > 15):
print("Username must be between 8 and 15 characters.")
else:
if(username.isalnum() == False):
print("Username must contain only alphanumeric characters.");
else:
count = 0;
inx = 0;
for ch in username:
if(ch.isdigit() and inx == 0):
print("The first character in your username cannot be a digit")
count = -1
break
if(ch.isdigit() and inx > 0):
count = 1
break
inx = inx+1
if(count == 1):
for ch in username:
if(ch.islower()):
count = 2
break
if(count == 2):
for ch in username:
if(ch.isupper()):
count = 3
break
if(count == 0):
print("Your username must contain at least one digit")
if(count == 1):
print("Your username must contain at least one lowercase character")
if(count == 2):
print("Your username must contain at least one uppercase character")
if(count == 3):
valid = True
if(valid == True):
print("Your username is valid!")
--------------output------------------
Please enter a username:foo
Username must be between 8 and 15 characters.
Please enter a username:foooooooooooooooo
Username must be between 8 and 15 characters.
Please enter a username:foo oo oo oo
Username must contain only alphanumeric characters.
Please enter a username:foooooooo
Your username must contain at least one digit
Please enter a username:1fooooooooo
The first character in your username cannot be a digit
Please enter a username:fooooooo1
Your username must contain at least one uppercase character
Please enter a username:Foooooooo1
Your username is valid!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.