0
Metacharacters
Hi so this is the question Can someone help to explain why my code wrong and correct it? Let's imagine we are creating our own authentication system. Create a program that takes a password as input and returns "Password created" if - it has at least one uppercase character - it has at least one number The Program should output "Wrong format" if requirements above are not met. Sample Input Hal44gb8 Sample Output Password created Code: import re password = input() #your code goes here pattern= r"(A-Z){1,},(a-z)*, (0-9){1,}" if re.match(pattern, password): print("Password created") else: print("Wrong format")
11 Antworten
+ 5
I don't know of a way to combine regular expressions to ensure all of the expressions are found but the following code would get the behaviour you describe. With the commas, it looks like you want each and every pattern matched in parallel which I don't know how to do.
The following will check that the provided string contains at least 1 upper case letter and 1 digit. I removed your reference to lower case letters (a-z)* completely because lower case letters were not mentioned in the requirements.
import re
password = input()
#your code goes here
patterns = [r"[A-Z]+", r"[0-9]+"]
wrong_format = False
for pattern in patterns:
if not re.search(pattern, password):
wrong_format = True
break
if wrong_format:
print("Wrong format")
else:
print("Password created")
Your attempt at the regular expression makes me think of slightly different requirements. If you want passwords to start with an upper case letter, contain a possibly empty sequence of lower case letters, and end with a single digit(ie. "Asdjh1" or "B5"), you could change your regular expression to r"^[A-Z][a-z]*[0-9]quot; Here is complete code:
import re
password = input()
pattern = r"^[A-Z][a-z]*[0-9]quot;
if re.match(pattern, password):
print("Password created")
else:
print("Wrong format")
+ 5
pattern = r"[A-Z]+[a-z]*[0-9]+"
+ 3
import re
Password = input()
pattern = "[A-Z][a-z]*[0-9]"
if re.match(pattern,Password):
print("Password created")
else:
print("Wrong format")
+ 1
import re
password = input()
#your code goes here
pattern = r"[A-Z*][0-9]*"
if re.match(pattern, password):
print("Password created")
else:
print ("Wrong format")
+ 1
import re
password = input()
#your code goes here
May=r"[A-Z]"
Num=r"[0-9]"
if re.search(May,password):
if re.search(Num,password):
print("Password created")
else:
print("Wrong format")
+ 1
import re
password = input()
#your code goes here
pattern = r".*[A-Z]+.*[0-9]+" #0+ character, 1+ Uppercase, 0+ character, 1+ digit
if re.match(pattern, password):
print("Password created")
else:
print ("Wrong format")
0
3
import re
password = input()
#your code goes here
pattern = r"[A-Z*][0-9]*"
if re.match(pattern, password):
print("Password created")
else:
print ("Wrong format")
0
Let's imagine we are creating our own authentication system.
Create a program that takes a password as input and returns "Password created" if
- it has at least one uppercase character
- it has at least one number
The Program should output "Wrong format" if requirements above are not met.
Sample Input
Hal44gb8
Sample Output
Password created
0
import re
password = str(input())
pattern1=r"[0-9]+"
pattern2=r"[A-Z]+"
if re.search(pattern1,password) and re.search(pattern2,password):
print("Password created")
else:
print("Wrong format")
0
import re
password = input()
pattern = r".*[A-Z].*[0-9]+.*"
if re.match(pattern, password):
print("Password created")
else:
print("Wrong format")
- 1
@Adam Dirker
Thank you for your solution! I was curious, though. At first I tried
"[A-Z]*[0-9]*"
and some of the cases were wrong. However, when I switched it to
"[A-Z*][0-9]*"
like you had it, it worked. If you don't mind, could you explain the difference between the two? Why is it only the A-Z that has the asterisk inside the brackets and not the 0-9?