question : Phone Number CheckingAlice has been having
trouble with people filling her website’s form incorrectly,
especially with phone numbers. She’d like tomake sure a user’s
phone number input only has digits (other than -) and is exactly 10
digits long.Finishvalidphonenumber(string)such that the function
returns True if a phone number is valid, and False if
not.
def valid_phone_number(number: str) -> bool:
"""
Return True if the number is a valid phone
number. A valid phone number:
1) Has exactly 10 digits (we are excluding the
country code)
2) Only has digits (no alphabets or special
characters)
3) May have "-" to split numbers. Example:
0123456789 and 012-345-6789 are
BOTH valid
Do *not* use loops for this problem. Try to make sure you only use built-ins
Hint: you will need 3 built-in string methods
>>>
valid_phone_number("0123456789")
True
>>>
valid_phone_number("012-345-6789")
True
>>>
valid_phone_number("01-23-45-67-89")
True
"""
# TODO: do some checking here
# change this
return True
#### Part 2: String Functions with loops ###
def valid_phone_number(number: str) -> bool: """ Return True if the number is a valid phone number. A valid phone number: 1) Has exactly 10 digits (we are excluding the country code) 2) Only has digits (no alphabets or special characters) 3) May have "-" to split numbers. Example: 0123456789 and 012-345-6789 are BOTH valid Do *not* use loops for this problem. Try to make sure you only use built-ins Hint: you will need 3 built-in string methods >>> valid_phone_number("0123456789") True >>> valid_phone_number("012-345-6789") True >>> valid_phone_number("01-23-45-67-89") True """ number = number.replace('-', '') return len(number) == 10 and number.isdigit()
Get Answers For Free
Most questions answered within 1 hours.