Trying to solve online shop in python core | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

17th Apr 2021, 1:13 PM
Simisola Osinowo
Simisola Osinowo - avatar
11 Answers
+ 10
import re Id = input() pattern = r"^[A-Z][A-Z][0-9][0-9]
quot; if re.search(pattern, Id): print('Searching') else: print('Wrong format') #the Code is Correct
6th May 2021, 6:28 PM
Verjin V
Verjin V - avatar
+ 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 "
quot; Symbol works. https://www.programiz.com/JUMP_LINK__&&__python__&&__JUMP_LINK-programming/regex
17th Apr 2021, 2:18 PM
Abhay
Abhay - avatar
+ 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"
17th Apr 2021, 2:08 PM
Abhay
Abhay - avatar
+ 2
Okay So how do i restrict it to checking 4 symbols
17th Apr 2021, 2:13 PM
Simisola Osinowo
Simisola Osinowo - avatar
+ 1
Thanks
17th Apr 2021, 2:33 PM
Simisola Osinowo
Simisola Osinowo - avatar
+ 1
import re Id = input() #your code goes here pattern = r"^[A-Z][A-Z][0-9][0-9]
quot; if re.search(pattern, Id): print('Searching') else: print('Wrong format')
19th Apr 2021, 10:07 AM
Gunjan Patel
Gunjan Patel - avatar
+ 1
import re Id = input() pattern = r"^[A-Z][A-Z][0-9][0-9]
quot; if re.search(pattern, Id): print('Searching') else: print('Wrong format')
23rd May 2022, 1:53 AM
Najwan Najmussabah
0
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))
17th Apr 2021, 4:38 PM
Amir
Amir - avatar
0
just a minor change =>if re.search(pattern,str(Id)): and it works
10th Jul 2021, 5:29 AM
Trishanku Rajkhowa
0
import re Id = input() pattern = r"['A-Z']['A-Z']['0-9']['0-9']" if len(Id)>4: print("Wrong format") elif re.search(pattern, Id): print("Searching") else: print("Wrong format") #this will work i think
4th Nov 2022, 4:01 PM
Siddharth Shivwanshi
Siddharth Shivwanshi - avatar
0
import re Id = input() if re.search(r"[A-Z][A-Z][0-9][0-9]
quot;, Id): print("Searching") else: print("Wrong format") Элементарно ватсон)
22nd Feb 2023, 11:35 AM
Наталья Бойко
Наталья Бойко - avatar