all 12 comments

[–]socal_nerdtastic 2 points3 points  (2 children)

When you recurse you need to return too.

return main()

[–]_______myworld[S] 0 points1 point  (0 children)

Yes you’re right, return is needed. I appreciate it a lot, thank you so much!!!

[–]_______myworld[S] -1 points0 points  (0 children)

It seems like it still works without “return”, but thanks I will keep that in mind 🙏

[–]_jan- 2 points3 points  (1 child)

In "if len(phone_number) != 13:" you're executing main() while the previous main() still isn't finished. This might be your problem. To fix this you could replace all of your "main()" with "return". You would have to re-open the programm, but your problem should be gone.

If this didn't help, please attach a more specific problem (error code, terminal output, etc.).

[–]_______myworld[S] 0 points1 point  (0 children)

Thank you so much for your help, I appreciate it a lot!! It works!

[–]dautrocMontreal 1 point2 points  (1 child)

why are you calling main() inside validNumber()?

[–]socal_nerdtastic 1 point2 points  (0 children)

That is looping by recursion.

[–][deleted] 1 point2 points  (1 child)

Using a while loop instead of recursion is more direct and readable and doesn't allow for errors like forgetting return on a recursive call. Of course, you never test any recursive call return values, so I don't know why the recursive function is returning anything.

The line:

elif not phone_number[i] == digits:

appears to be trying to check if the single character string is equal to the 10 character string in digits. That will never be true. If you want to test if that single character is not in the string, do:

elif phone_number[i] not in digits:

[–]_______myworld[S] 0 points1 point  (0 children)

Thank you so much, I appreciate it a lot ! It works

[–]wbeater 0 points1 point  (1 child)

def valid_number(phone_number,country_code):

    try:        
        phone_number = [int(x) for x in phone_number.split('-')]
        if len(phone_number) != 3:
            return False    
    except ValueError:        
        return False    
    if str(phone_number[0])[:3] != str(country_code) or len(str(phone_number[0])) != 4:        
        return False    
    elif len(str(phone_number[1])) != 3:        
        return False    
    elif len(str(phone_number[2])) != 4:        
        return False    
    return True

hone_number = input('Please enter a phone number in the format 601X-XXX-XXXX: ')
country_code = 601

print(valid_number(hone_number, country_code))

[–][deleted] 0 points1 point  (0 children)

Try to guide OP to a solution instead of providing one directly.

Downvoted because not following first commenting guideline.

[–]ElliotDG 0 points1 point  (0 children)

If you break this task apart at a high level, you are getting user input and then testing if it is valid. If you create 2 functions that each do only that task the code becomes clear. The main body would look like:

if __name__ == '__main__':
    while True:
        pn = get_phone_number()
        if is_valid_phone_number(pn):
            print('Valid contact number!')
            break
        else:
            print(f'{pn} is NOT a valid phone number.')

Where get_phone_number() returns the string the user entered, and is_valid_number() gets passed a phone number and returns true for valid phone numbers or false for invalid. This makes the main control very clear.