Hi this is RM MUSIC.
Currently, working on a project regarding username and password validation system, which consist of storing data in my dictionary.
My code is written below:
user_details = {
"user1": "user1234"
}
print("Are you a new user or an existing user")
user_type = str.lower(input())
if user_type == "new user":
user_new_name = input("Enter your new user name: ")
user_new_name_count = 0
user_new_name_count_session_limit = 5
user_new_name_valid = False
Now I want to add the feature that how can I create a while or for loop in order to at first fetch the new username data and can differentiate between the correct and the wrong typed data.
Correct data would include:
- The username should be between 6 to 10 characters long.
- The username index[0] should have a capital case.
- The username should consist of digits in some place,.
- The username should have a special character.
I want these all to be specified in the IF statements which will be inside by while/for loops, so that I the input box is just shown for five times in front of the user, whenever he types out of the criterion mentioned in my correct data and after that the user will timed out of the session.
I want to solve this problem as soon as possible.
So please some one help me for this.
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>
) will also add backticks around text.
Note: Backticks are not single quotes.
1 Like
Thanks for the information.
import re
class User:
def __init__(self):
user_type = str.lower(input('Are you a new or existing user? [New User, Existing User]: '))
if user_type == 'new user':
self.create()
def create(self):
attempts_allowed = 5
while attempts_allowed:
validated = self.validate(input('Enter your desired username: '))
if validated:
break
else:
attempts_allowed -= 1
def validate(self, username):
valid = True
if len(username) < 6 or len(username) > 10:
print('Username must be between 6 and 10 characters')
valid = False
if username[0] != str.upper(username[0]):
print('Username must begin with a capitalized letter')
valid = False
match = re.search(r'\d', username)
if not match:
print('Username must have a number')
valid = False
match = re.search(r'[!@#$%^&*]', username)
if not match:
print('Username must have a special character in it [!@#$%^&*]')
valid = False
return valid
user = User()
2 Likes
Thanks for your reply. I found it informative and it also solve my problems