Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, I'm trying to make this variable "username" go through all these conditions, however, I can't seem to figure out the last condition - making sure the variable has at least one digit.

Any help would be greatly appreciated!


Python
username = input("Please enter a username: ")



while len(username) < 8 or len(username) > 15:
    print("Username must be between 8 and 15 characters long.")
    username = input("Please enter a username: ")
while username.isalnum() == False:
    print ("Username can only contain alphanumeric characters.")
    username = input("Please enter a username: ")
while username.islower() == True:
    print ("Usernames must contain at least one uppercase character")
    username = input("Please enter a username: ")
while username.isupper() == True:
    print ("Usernames must contain at least one lowercase character")
    username = input("Please enter a username: ")
while username[0].isnumeric() == True:
    print ("The first character in a username cannot be a digit")
    username = input("Please enter a username: ")
while username[-1].isnumeric() == True:
    print ("The last character in a username cannot be a digit")
    username = input("Please enter a username: ")   
while any(username.isdigit()) == False:
    print ("Usernames must contain at least one numeric character") # can't figure this one out
    username = input("Please enter a username: ")
    break


What I have tried:

I've tried what you see above, also finding a way around it by making a condition like ".isalpha" and making it test false, but I just don't know how to test if only one of the characters in the string are a number.
Posted
Comments
Richard MacCutchan 4-Apr-16 3:56am    
A string Is just an array of characters, so you can loop through them and test for the conditions you want.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900