+ 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
5 Réponses
+ 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
+ 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")
0
Thank you so much lol you were right about "quot; sign, i almost forgot that.
《 Nicko12 》
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")
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")