+ 3

HELP!!

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 My code : import re def valid(s): pattern = re.compile("^[189]\d\d\d\d\d\d\d") return pattern.match(s) s = input("") if (valid(s)): print("Valid") else: print("Invalid") When I run this in sololearn terminal while submitting my project it works completely fine but still got 1 error and I'm not able to find that 1 little error. Idk whats wrong with this code pls let me know whats wrong and do correct me! Thank you in advance

31st Dec 2020, 3:43 PM
Roshan Rasal
Roshan Rasal - avatar
5 Antworten
+ 6
I think you forgot the $ sign which indicates the end, it must be used because you have "^" which indicates start. And just additional info, instead of typing " \d " 7 times you can just do " \d{7} " 7 inside the bracket means the number of repetition. I hope this helps. If you need more explanation, feel free to ask. Thanks and Happy Coding! Your Code: https://code.sololearn.com/c6Ao4xUiYCNv/?ref=app
31st Dec 2020, 3:48 PM
noteve
noteve - avatar
+ 3
import re def valid(s): pattern = re.compile("^[189]\d{7}
quot;) return pattern.match(s) s = input("") if (valid(s)): print("Valid") else: print("Invalid")
12th Jan 2022, 4:57 AM
dilfuza inomova
0
Thank you so much lol you were right about "
quot; sign, i almost forgot that. 《 Nicko12 》
31st Dec 2020, 4:06 PM
Roshan Rasal
Roshan Rasal - avatar
0
Phone Number Validator ::: 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 program : import re def valid(s): pattern = re.compile("^[189]\d{7}
quot;) return pattern.match(s) s = input("") if (valid(s)): print("Valid") else: print("Invalid")
15th Jun 2021, 10:43 AM
Madhavareddy
Madhavareddy - avatar
0
import re def valid(s): pattern = re.compile("^[189]\d{7}
quot;) return pattern.match(s) s = input("") if (valid(s)): print("Valid") else: print("Invalid")
5th Jul 2021, 2:16 PM
Shanmuga Priya
Shanmuga Priya - avatar