Python Regular Expression Practice Quiz "Authentication!"
Hey there, I have some Problems at the Python Practice Quiz "Authentication!" the input is a Password which shall has -at least one uppercase character and at least one number I got some help from google to solve it and surprisingly I really passed this Quiz but the Code is not right and I have no Idea how to change it, that it works in the right way. The Problem is: the most Password inputs are recognized correctly (see Examples below) but for example the Password "aA1" which also contains a uppercase character and one number, it shows "wrong format". Thanks for your Help my Code: import re password = input() pattern = r"\d.*[A-Z]|[A-Z].*\d" if re.match(pattern, password): print("Password created") else: print("Wrong format") Example Inputs -> Outputs: Aa1 ->Password created A1a ->Password created 1Aa ->Password created 1aA ->Password created aA1 ->Wrong format -->should be "Password created" a1A ->Wrong format -->should be "Password created"
1/4/2021 6:49:11 PMJochen Müller
12 AnswersNew Answer
Thanks for your replies I thought this is going to be a a easier Quiz because it is more at the beginning of the Regular Expression Chapter. The solutuion from Lisa maybe not that Elegant but at least it worked as it should. This helped me a lot. thanks this is now my code which is working in the right way: import re passwords = ["Aa1", "A1a", "1Aa", "1aA", "a1A", "aA1", "aa1", "aab"] for p in passwords: number = re.search("[0-9]",p) LETTER = re.search("[A-Z]",p) if bool(number) is True and bool(LETTER) is True: print("Password created") else: print("Wrong format") Still curious how the single regex would look like ;)
Calvin Sheen Thomas Yeah that was the regex the OP used and was having issues. I ended up using two expressing numPat = r”[0-9]” upperPat = r”[A-Z]” if re. search(numPat, password) and re.search(upperPat, password) I haven’t sorted a way to get it all in one expression
It tried around and now my regular expression is: pattern = r"[A-Z0-9]" and it works exactly as my previous!? ( aA1 and a1A ->Wrong format) So now I have a shorter Expression but still the same Problem :D
This will help you: https://code.sololearn.com/car0nA5fk805/?ref=app
Seems to be a pro quiz 🤔 Is it necessary to use a single regex? If not we could work around by using one pattern to check for number and one for capital letters (though not very elegant) passwords = ["aA1", "1Aa", "A11", "a1a", "Aaq"] for p in passwords: number = re.search('[0-9]', p) LETTER = re.search('[A-Z]', p) print(p, bool(number), bool(LETTER))
I adapted the example from https://www.tutorialspoint.com/How-to-check-if-a-string-has-at-least-one-letter-and-one-number-in-Python to combi = '^(?=.*[0-9])(?=.*[A-Z])' and then tried re.match()
Here you go: from re import search as a b,c=r"([A-Z]+\w*\d)|(\d+\w*[A-Z])",input() d="Verified" if a(b,c) else "Not Verified" print(d) #Might as well copy and paste it in the #playground.
Hey Jochen, Not sure if you sorted this out yet. Have you tried using re.search() instead of re.match()? re.match() starts at the beginning of the string and will return false if their isn't an immediate match. re.search() looks at entire string for a match. I think pattern = r"[A-Z0-9]" checks for the characters in that order. HAven't figured this out with single regex yet
Cody Knight Your regex expression doesn't give the required results; it just searches for a string which has an upper-case letter OR an integer.
Yup, doing this with two regex is easy. Here's the single regex version: r"([A-Z]+\w*\d)|(\d+\w*[A-Z])" This works but using two regex is the preferable method for me.
Maybe? pattern = r"\d.*[A-Z]|[a-z].\*d"
with this Code Aa1 and A1a also shown as Wrong Format