Answer:
#function to validate username
def valid_username(username):
#checking is length is between 8 to 15
length_valid=True
if(len(username)<8 or len(username)>15):
length_valid=False;
#checking is string is alphanumeric
alphanumeric=username.isalnum()
#checking first_and_last Characters of string is not digit
first_and_last=True
if(username[0].isdigit() or username[len(username)-1].isdigit()):
first_and_last=False
#counting uppercase lowercase and numeric Characters in string
uppercase=0
lowercase=0
numeric=0
for i in username:
if(i.isdigit()):
numeric=numeric+1
elif(i.isalpha()):
if(i.isupper()):
uppercase=uppercase+1
else:
lowercase=lowercase+1
valid_no_char=True
if(uppercase<1 or lowercase<1 or numeric<1):
valid_no_char=False
#printing the result
print("Length of username: "+str(len(username)))
print("All Characters are alpha-numeric: "+str(alphanumeric))
print("First and last Character are not digit: "+str(first_and_last))
print("no of uppercase characters in the username: "+str(uppercase))
print("no of lowercase characters in the username: "+str(lowercase))
print("no of digits in the username: "+str(numeric))
#checking string is Valid or not
if(length_valid and alphanumeric and first_and_last and valid_no_char):
print("Username is Valid\n")
return True
else:
print("username is invalid, please try again\n")
return False
#loop runs until valid_username enterd by user
while(True):
username=input("Enter a username: ")
if(valid_username(username)):
break
Explanation:
The code was created by considering all the required cases asked in the question.
In this program, I asked username from the user continuously until it enters a valid username
first it check for length of string
then check is string contains only alphanumeric characters
then first and last digit is or not
than no of uppercase lowercase and numeric character
using the above parameter declare username valid or not