[SOLVED] Python Phone Number Validator | Sololearn: Learn to code for FREE!

+13

[SOLVED] Python Phone Number Validator

Hi! This is the only challenge I need to do, to finish the python course. The valid number starts with 1or8or9 and has the length of 8. My code passed the first 4 tests, but it failed the 5th test. I can't see the phone number in the 5th test. What can be the problem? Current code: import re number = input() if re.search("([189])[\d]+", number) and len(number) == 8: print("Valid") else: print("Invalid")

11/13/2020 12:54:53 PM

Csongor Kertesi

41 Answers

New Answer

+4

https://code.sololearn.com/cA3A20a15A2a You can find answer in the above link.

+32

import re pattern = r"^[189][0-9]{7}$" if re.match(pattern, input()): print("Valid") else: print("Invalid") =========================================== I hope this helps you all. ^ is for starts with [189], [0-9] digits for {7} repetiotions $ to end total of [189] + [0-9]{7} which is equals to 8. Hope you have understood my answer😊

+8

import re a = input() pattern = r"[189]" if re.match(pattern,a): if len(a) == 8: print("Valid") else: print("Invalid") else: print("Invalid")

+4

import re #your code goes here number = input() pattern = r"^(1|8|9)" match = re.match(pattern, number) if match and len(number) == 8: print("Valid") else: print("Invalid")

+4

import re #your code goes here number = input() if re.match(r"^[1|8|9]\d{7}$", number): print("Valid") else: print("Invalid")

+3

import re pattern = r"^[189][0-9]{7}" str = input() match = re.search(pattern, str) if match: print("Valid") else: print("Invalid") What wrong with this code?

+3

i still get one case not correct this is my code where is the mistake ? import re #your code goes here pattern = r"^1|8|9 + [0-9]{7}$" num = str(input()) if re.search(pattern, num): print("Valid") else: print("Invalid")

+3

Most probably you're getting this failure in last test case cause re.search() searches for the whole string even if the string contains multi-lines and tries to find a match of the substring in all the lines of string but you want your numeric number to only START with 1,8 or 9. better to use re.match() instead

+3

Here is my answer to that guys: import re number = input() pattern = r"^[189][0-9]" match = re.match(pattern, number) if len(number) == 8 and match: print("Valid") else: print("Invalid")

+3

@Arya Huda Arrasyid You need a $ at the end of your regex, otherwise, the phone number can start with the correct 8 digits but then go on indefinitely. It can only be 8 digits

+2

I recommend to test the code on the SL Playground with a few data inputs and a few print statements and see what happend and solve that. Sp you could see that 01234567 gives a valid number, what unfortunately is not true from task description. The solution for it will be to code that the number has to begin with 1 or 8 or 9.

+2

import re num = input () pattern = r"^[1|8|9][0-9]*$" if re.match(pattern,num) and len(num)==8: print("Valid") else: print("Invalid")

+2

I think this is simplest: import re phone = input() pattern = r"^[189][0-9]{7}$" if re.match(pattern, phone): print("Valid") else: print("Invalid")

+2

import re #tu código va aquí pattern = "\d\1|8|9{1,9}$" number = input() if re.search(pattern, number) and len(number) == 8: print("Valid") else: print("Invalid")

+2

Try this: import re #your code goes here str = input() pattern = r"^(1|8|9)+\d{7,7}$" if re.match(pattern , str): print("Valid") else: print("Invalid")

+1

import re print("Valid" if re.match(r'[789]\d{7}$', input()) else 'Invalid')

+1

Okime Obia It's the python library for regular expressions

+1

Mi respuesta es: import re pattern = r"(1|8|9)[0-9]{7}$" entrada = input() if re.match(pattern, entrada): print("Valid") else: print("Invalid")

+1

import re num = input () pattern = r"^[1|8|9][0-9]*$" if re.match(pattern,num) and len(num)==8: print("Valid") else: print("Invalid")

+1

import re x=input() if re.fullmatch("[189]\d{7}",x): print('Valid') else: print("Invalid")