Python Forum

Full Version: 'str' object is not callable
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
this is my script below :
when i first run, everything is normal and prompt user "Do you want a new reading :"
but when i key in yes and the error 'str' object is not callable came out on this line "hex_number = str(hex(randomnumber))"
any idea how to solve this issue? i've check and confirm i dont have another str define else where ..

def main():    

    import random
    global hex
    

    randomnumber = random.randint(0,99)

    hex_number = str(hex(randomnumber))
    hex_numberd0 = '0x' + hex_number[2:]
    print('A random D0:', hex_number)

    randomnumber = random.randint(0,99)
    hex_number = str(hex(randomnumber))
    hex_numberd1 = '0x' + hex_number[2:]
    print('A random D1:', hex_number)

    randomnumber = random.randint(0,99)
    hex_number = str(hex(randomnumber))
    hex_numberd2 = '0x' + hex_number[2:]
    print('A random D2:', hex_number)

    D0 = hex_numberd0
    D1 = hex_numberd1
    D2 = hex_numberd2

    #hex coversion
    hex= D0
    dec0 = int(hex,16)
    print('Value in hexadecimal D0 :', hex)
    print('Value in decimal D0 :', dec0)

    hex= D1
    dec1 = int(hex,16)
    print('Value in hexadecimal D1 :', hex)
    print('Value in decimal D1 :', dec0)

    hex= D2
    dec2 = int(hex,16)
    print('Value in hexadecimal D2 :', hex)
    print('Value in decimal D2 :', dec0)

    #coversion ends
    
    #inclination calculation

    if(dec2 >> 7 == 1):
           signbit = -1
    else:
            signbit = 1

    Data0 = (dec0 >> 6)
    Data1 = (dec1 <<2)
    Data2 = ((dec2&127)<<10)

    print("D0 =", Data0)
    print("D1 =", Data1)
    print("D2 =", Data2)

    DataI = signbit*(Data0 + Data1 + Data2)/1000
    print("inclination = ", DataI)

    restart = input("Do you want a new reading :   ").lower()
    if restart == "yes":
        main()

    else:
        exit()
main()
The built-in function hex is being overwritten.
(Jul-28-2021, 09:46 AM)Yoriz Wrote: [ -> ]The built-in function hex is being overwritten.


Hi,

any suggestions to work around?
Don't name your variable hex.

Also:

Do you really need a global? Those should be avoided most of the time. You don't seen to be using it at global scope anyway.

import statements should go at the top of the file, not inside functions or otherwise in the middle of other code. Rationale: it interrupts reading of that code.
(Aug-03-2021, 04:41 AM)ndc85430 Wrote: [ -> ]Don't name your variable hex.

Also:

Do you really need a global? Those should be avoided most of the time. You don't seen to be using it at global scope anyway.

import statements should go at the top of the file, not inside functions or otherwise in the middle of other code. Rationale: it interrupts reading of that code.

Hi!

if i did not declare a global in it, this error will appear "local variable 'hex' referenced before assignment"
i've shifted the statements on top too, thanks for the advice on this !
Please remember to post errors in their entirety, because they give important information about the problem (i.e. line number and a pointer to the problem), along with the code that causes them. It's important for us to see what you're doing, rather than trying to recreate the problematic code ourselves.
(Aug-03-2021, 05:58 AM)ndc85430 Wrote: [ -> ]Please remember to post errors in their entirety, because they give important information about the problem (i.e. line number and a pointer to the problem), along with the code that causes them. It's important for us to see what you're doing, rather than trying to recreate the problematic code ourselves.


 import random
def main():   
    randomnumber = random.randint(0,99)
 
    hex_number = str(hex(randomnumber)) ** [color=#C0392B]"local variable 'hex' referenced before assignment"** error appear here[/color]
    hex_numberd0 = '0x' + hex_number[2:]
    print('A random D0:', hex_number)
 
    randomnumber = random.randint(0,99)
    hex_number = str(hex(randomnumber))
    hex_numberd1 = '0x' + hex_number[2:]
    print('A random D1:', hex_number)
 
    randomnumber = random.randint(0,99)
    hex_number = str(hex(randomnumber))
    hex_numberd2 = '0x' + hex_number[2:]
    print('A random D2:', hex_number)
 
    D0 = hex_numberd0
    D1 = hex_numberd1
    D2 = hex_numberd2
 
    #hex coversion
    hex= D0
    dec0 = int(hex,16)
    print('Value in hexadecimal D0 :', hex)
    print('Value in decimal D0 :', dec0)
 
    hex= D1
    dec1 = int(hex,16)
    print('Value in hexadecimal D1 :', hex)
    print('Value in decimal D1 :', dec0)
 
    hex= D2
    dec2 = int(hex,16)
    print('Value in hexadecimal D2 :', hex)
    print('Value in decimal D2 :', dec0)
 
    #coversion ends
     
    #inclination calculation
 
    if(dec2 >> 7 == 1):
           signbit = -1
    else:
            signbit = 1
 
    Data0 = (dec0 >> 6)
    Data1 = (dec1 <<2)
    Data2 = ((dec2&127)<<10)
 
    print("D0 =", Data0)
    print("D1 =", Data1)
    print("D2 =", Data2)
 
    DataI = signbit*(Data0 + Data1 + Data2)/1000
    print("inclination = ", DataI)
 
    restart = input("Do you want a new reading :   ").lower()
    if restart == "yes":
        main()
 
    else:
        exit()
main()[python]
[/python]
This is because again, you're using hex as a variable name later on and overwriting the built in function. So, don't do that.

Also, I should have mentioned, but post code within "[python]" tags to preserve indentation and formatting. You can press the Python logo button to put a pair of tags in your post.
The hex function returns a string. You don't need to do this: str(hex(randomnumber))

If you want to do something multiple times, use a loop. If you want to store multiple values use a list.
import random

hex_numbers = [f'0x{hex(random.randint(0, 99))[2:]}' for _ in range(3)]
numbers = [int(hex_number, 16) for hex_number in hex_numbers]
for hex_number, number in zip(hex_numbers, numbers):
    print(hex_number, '=', number)