Hanqing Zhao
Hanqing Zhao

Reputation: 25

type error when casting sting type to integer type using python 3

I write code to achieve the effect like that if you write 123 then return 321, if -123 then return -321. However, after I finished the code and try to run it the sentence of | int = -int(rev_str) | gives me the error saying: "'int' object is not callable". I do not know why, please anyone help me. thank you so much and appreciate it.

def reverse_32_int(int):
    if (int < -(2**31)) or (int > 2**31 - 1):
        print('exceed 32-bit range')
    else:
        if int < 0:
            int = -int
            str_int = str(int)
            rev_str = str_int[::-1]
            int = -int(rev_str)
            return int
        elif int > 0:
            str_int = str(int)
            rev_str = str_int[::-1]
            int = int(rev_str)
            return int
        else:
            return 0

reverse_32_int(123)

Upvotes: 0

Views: 47

Answers (1)

furas
furas

Reputation: 143097

You used int as variable in def reverse_32_int(int). You can't have variable int and function int(). Rename it.

I use variable value instead of int

def reverse_32_int(value):
    if (value < -(2**31)) or (value > 2**31 - 1):
        print('exceed 32-bit range')
    else:
        if value < 0:
            value = -value
            str_int = str(value)
            rev_str = str_int[::-1]
            value = -int(rev_str)
            return value
        elif value > 0:
            str_int = str(value)
            rev_str = str_int[::-1]
            value = int(rev_str)
            return value
        else:
            return 0

reverse_32_int(123)

Upvotes: 2

Related Questions