0

This is my first coding class and I'm a little confused...

Im looking to write a program that prompts for a phone number and determines whether or not it's a valid 10-digit number by ignoring any punctuation. I have to write a function that takes the phone number string as a parameter and returns True if it is valid, and False if not. I also have to use a loop to iterate over the string and increment counter whenever I see a digit.

I'm not sure this is correct , but this is what I came with so far. I'm not sure how to create a loop to iterate over a string to determine True or False phone numbers.

main():
    phone_number= input("Please enter a phone number in the format XXX-XXX-XXXX: ")
    validNumber(phone_number)

    def validNumber(phone_number):
        for i,c in enumerate(phone_number):
            if i in [3,7]:
                if c!= "-":
                   phone_number=input("Please inter a valid phone number:")
                   return False
            elif
ptierno
  • 9,534
  • 2
  • 23
  • 35
Sav
  • 13
  • 2
  • Seems like you're doing this the hard way. You may want to look at the regular expression package named `re`. example `r = re.compile('[0-9][0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]')` then to check you would use `if r.match(phone_number) is None:` – jodag Jul 11 '17 at 03:00
  • Based on your requirements you would could do something like `if c in ['0','1','2','3','4','5','6','7','8','0']:` to check for digits. – jodag Jul 11 '17 at 03:03
  • The answers to [this question](https://stackoverflow.com/questions/3868753/find-phone-numbers-in-python-script) may help – Daniel Corin Jul 11 '17 at 03:05
  • So if I change my if c in.... how do I determine to the user if the number is valid or not? – Sav Jul 11 '17 at 03:05

2 Answers2

0

How about think the solution with step-by-step?

  1. Check the number of "-" that split the number to three part.
  2. Check the length of each XXX(or XXXX) at XXX-XXX-XXXX, if the each XXX has appropriate length(3, 3 and 4)
  3. Check if each XXX(or XXXX) is decimal or not.

The code can be like:

def validNumber(phone_number):
    p_num_list = phone_number.split('-') # get list of each XXX

    if len(p_num_list) is not 3: # if the number has 3 part
        return False
    else:
        if len(p_num_list[0]) is not 3: # Check length of each part
            return False
        if len(p_num_list[1]) is not 3:
            return False
        if len(p_num_list[2]) is not 4:
                return False
        if p_num_list[0].isdecimal() and p_num_list[1].isdecimal() and p_num_list[2].isdecimal(): # check if each part is decimal
            return True
    return False

if __name__ == '__main__':
    p_num = input("Enter the phone number : ")
    print(validNumber(p_num))
  • 1
    It can be replace by more easier(or high quality) code, but i recommend to start the code with think like 'How can i solve this problem?' or 'How can i solve the problem step-by-step?'. – DevFallingstar Jul 11 '17 at 03:17
  • 1
    So i wrote this code to help you to get more helpful coding class. Remember that the good codes are come from good thinking. – DevFallingstar Jul 11 '17 at 03:19
  • Thank you for breaking this down for me. I'm beginning to understand when the code is broken up into sections. I am still receiving an invalid syntax for the colon on the seventh line of code. Any suggestions? – Sav Jul 11 '17 at 03:23
  • @Sav No problem. But first, can you comment the contents of syntax error? – DevFallingstar Jul 11 '17 at 03:25
  • @Sav Also i've edited my answer cuz it had wrong indentation. – DevFallingstar Jul 11 '17 at 03:27
  • I figured it out! Takes lots of practice, thank you for your help! – Sav Jul 11 '17 at 04:04
  • @Sav Cool! If you got helpful information from my answer, please check the mark at the left-above of post. Have a good day :) – DevFallingstar Jul 11 '17 at 04:26
0

You could try something like this:

def valid_number(phone_number):
    try:
        number = [str(int(i)) for i in phone_number.split('-')]
    except:
        print('Error, only numbers allowed')
        number = ''
    if len(''.join(number)) == 10:
        print('This is a valid phone number')
        return True
    else:
        print('This is not a valid phone number')
        return False

def main():
    phone_number = input('Enter number in format xxx-xxx-xxxx: ')
    valid_number(phone_number)

if __name__ == '__main__':
    main()

int() is trying to convert the string into an integer, if it fails, its not a number, then it checks whether the number is 10 characters long or not returning then True or False.

José Garcia
  • 136
  • 9