Trying to solve online shop in python core | Sololearn: Learn to code for FREE!

+1

Trying to solve online shop in python core

import re Id = input() pattern=r"[A-Z][A-Z][0-9][0-9]" if re.search(pattern,Id): print("Searching") else: print("Wrong format") One test case out of 5 seem to come out wrong and i don't know why All the products in online shop have their own ID. Every ID consists of 4 symbols: The first symbol: an uppercase character The second symbol: an uppercase character The third symbol: a digit The forth symbol: a digit Write a program for a search tool, that will take the ID as input and output "Searching" if the format is correct, and "Wrong format", if it's not. Sample Input LG17 Sample Output Searching

4/17/2021 1:13:24 PM

Simisola Osinowo

8 Answers

New Answer

+5

Simply check the length of it . Or you can use a pattern like this : ^[A-Z][A-Z][0-9][0-9]$ Take a look at the following article if you don't know how "^" and "$" Symbol works. https://www.programiz.com/python-programming/regex

+3

I can't test it but try checking for length of the inputted id as well since re.search will match those 4 symbols even in a input like "4ZA56" or "ZA567"

+2

Okay So how do i restrict it to checking 4 symbols

+1

+1

import re Id = input() pattern=r"\b[A-Z]{2}[0-9]{2}\b" element=re.findall(pattern,Id) if element : print("Searching") else: print("Wrong format") print (Id) print (re.findall(pattern,Id))

+1

import re Id = input() #your code goes here pattern = r"^[A-Z][A-Z][0-9][0-9]$" if re.search(pattern, Id): print('Searching') else: print('Wrong format')

0

import re Id = input() pattern = r"^[A-Z][A-Z][0-9][0-9]$" if re.search(pattern, Id): print('Searching') else: print('Wrong format') #the Code is Correct

0

just a minor change =>if re.search(pattern,str(Id)): and it works