简体   繁体   中英

I keep getting an error called 'int' object has no attribute 'replace'

For my code I don't need to replace numbers, only things like parenthesis and dashes and I thought I spec ifoed that in my code but it still doesnt work. Here is the code:

def standardize_phone_number(phone_number):
    phone_number = str(phone_number.replace('(', '')\
                         .replace(')', '')\
                         .replace(' ', '')\
                         .replace('-', ''))

    if not (phone_number.isdigit() and len(phone_number) == 10):
        return None

    if phone_number[0] == '(' and phone_number[4:6] == ')' and \
       phone_number[9] == '-' and len(phone_number) == 13:
        return phone_number.replace('(', '').replace(')', '-')

    if phone_number[3] == '-' and phone_number[7] == '-' and \
       len(phone_number) == 11:
        return phone_number.replace('-', '').replace('-', '')

    if phone_number[5] == ' ' and phone_number[9] == '-' and \
       len(phone_number) == 13:
        return phone_number.replace(' ', '').replace('-', '')
    else:
        return None
print(standardize_phone_number(123-456-7890))

The problem is that you're passing an int as input to standardize_phone_number , specifically the int 123-456-7890 , which is -8223 , while standardize_phone_number expects a string.

What you want instead is:

standardize_phone_number('123-456-7890')

Edit: On Returning None

With the input above, your code will reach the final else and therefore, return None . Since you strip out all occurrences of the characters '(' , ')' , ' ' , and '-' , the phone_number string will just be the digits entered (assuming a valid phone number format is entered). So none of your if conditions will ever evaluate to True .

It looks like you are meaning to remove all instances of the characters '(' , ')' , ' ' , and '-' from a phone number and just return the digits of the phone number. In that case you can reduce your code to:

def standardize_phone_number(phone_number):
    phone_number = str(phone_number.replace('(', '')\
                         .replace(')', '')\
                         .replace(' ', '')\
                         .replace('-', ''))

    if phone_number.isdigit() and len(phone_number) == 10:
        return phone_number
    else:
        raise ValueError("The number you entered is not a valid phone number")

With re.sub you can compact all those replace calls into one:

import re

def standardize_phone_number(phone_number):
    phone_number = re.sub(r'[()\s-]', '',  phone_number)
    if phone_number.isdigit() and len(phone_number) == 10:
        return phone_number
    else:
        raise ValueError("The number you entered is not a valid phone number")

Of course, the ValueError is optional, you can implement whatever handling you think is right for invalid phone numbers.

change you calling method

print(standardize_phone_number(123-456-7890))

from this to

print(standardize_phone_number('123-456-7890'))

Then it will work properly because only string object having the method "replace()"

Your code checks that phone_number is a digit, then returns none if it is not. After applying suggestions from this post, it will always return None since now phone_number will always be a string.

if not (phone_number.isdigit() and len(phone_number) == 10):
    return None

comment out those 2 lines, it should solve your recurring problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM