Phone Number Validator | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Phone Number Validator

Whats wrong with my code? You are given a number input, and need to check if it is a valid phone number. A valid phone number has exactly 8 digits and starts with 1, 8 or 9. Output "Valid" if the number is valid and "Invalid", if it is not. Sample Input 81239870 Sample Output Valid 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")

1st Jun 2022, 10:59 AM
Dragos
5 Answers
+ 3
Please re-read the lesson. Groups are formed with [], not with {} Justice It's the regex end-of-module project of Python Core course Dragos Please mention the course name when you refer to a sololearn task.
1st Jun 2022, 11:10 AM
Lisa
Lisa - avatar
+ 1
Lisa Ooooooooo, I see. Yeah, mentioning the Course/Lesson name would've saved me the questioning LOL
1st Jun 2022, 11:14 AM
Justice
Justice - avatar
0
Are you getting a particular error? Also, it's interesting you're using Regex. I would've just accessed the first character of the string. Ah, nevermind, syntax error like Lisa said.
1st Jun 2022, 11:10 AM
Justice
Justice - avatar
0
Thanks Lisa, it seems this was the problem.
1st Jun 2022, 11:14 AM
Dragos
0
another solution import re #your code goes here number = input() pattern = r"^[189]+\d{7}
quot; match =re.match(pattern,number) if match: print("Valid") else: print("Invalid")
4th Apr 2023, 4:02 PM
Nguyễn Thanh Lâm
Nguyễn Thanh Lâm - avatar