I have a task i need some help with. The task is to create a python script that asks the user to enter a desired username. The username has to be as following: "a11aaaaa". So starting with a letter, 2x numbers, 5x letters. This is the rule for how the username should look and if the given input does not match that, the user shall be able to try again until getting it right. Thankful for any help!
As I recently learnt from another user on SO, \w
includes \d
. Therefore, '^\w\d{2}\w{5}$'
, as suggested by some users here, will match, for example, 12345678
.
To fix that, just specify the character class explicitly:
import re
regex = re.compile('^[A-Za-z]\d{2}[A-Za-z]{5}$')
while True:
password = input('Please enter a password: ')
if regex.search(password):
print('Yay! Your password is valid!')
break
else:
print("Oh no, that's not right. You need a letter, then two numbers, then five letters. ", end='')