Python Forum
NameError: name 'hash_value_x_t' is not defined - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: NameError: name 'hash_value_x_t' is not defined (/thread-37139.html)



NameError: name 'hash_value_x_t' is not defined - Anldra12 - May-05-2022

Why happen the name hash_value_x_t not define
def setup():
    global hash_value_x_t
    """
    Generates public key and master secret key.
    :return:
    """
    if debug:
        print('\nSetup algorithm:\n')

    params = Parameters(qbits=512, rbits=160)  # type a
    pairing = Pairing(params)
    # G2 generator g
    g = Element.random(pairing, G2)  # 1024 bytes
    # x y
    x_ran = Element.random(pairing, Zr)  # 160 bits
    y_ran = Element.random(pairing, Zr)
    c1='1'+str(hash_value_x_t)
    c2 ='2'+ str(hash_value_x_t)
    hash_value_x_t = Element.from_hash(pairing, Zr,c1)
    print('11', hash_value_x_t)
    hash_value_x_t = Element.from_hash(pairing, Zr, c2)
    print('22', hash_value_x_t)

    print(hash_value_x_t)
Error:
Traceback (most recent call last): File "/home/ali/Downloads/MedShare2021-main/constant_abe.py", line 231, in <module> (pk, msk) = setup() File "/home/ali/Downloads/MedShare2021-main/constant_abe.py", line 26, in setup c1='1'+str(hash_value_x_t) NameError: name 'hash_value_x_t' is not defined



RE: NameError: name 'hash_value_x_t' is not defined - bowlofred - May-05-2022

You've said on line 2 that hash_value_x_t should refer to the global variable with that name. But it doesn't make sure that variable has been assigned yet. If it's not yet assigned, you can't read the value when line 17 executes. Here's a small program that does the same thing

def myfunction():
    global global_1
    global global_2

    x = global_1 # OK because global_1 was assigned before the function was called
    x = global_2 # Fails because global_2 not assigned before the function was called


global_1 = "a string"
myfunction()
Error:
Traceback (most recent call last): File "./global_test.py", line 10, in <module> myfunction() File "./global_test.py", line 6, in myfunction x = global_2 # Fails because global_2 not assigned before the function was called NameError: name 'global_2' is not defined



RE: NameError: name 'hash_value_x_t' is not defined - Anldra12 - May-06-2022

Yeah, you are right the actual code I found from GitHub gives me an error UnboundLocalError: local variable 'hash_value_x_t' referenced before assignment without global setup inside a function so I made a change to inside define fun(): set hash_value_x_t as global and get error hash_value_x_t is not defined.
def setup():

    """
    Generates public key and master secret key.
    :return:
    """
    if debug:
        print('\nSetup algorithm:\n')

    params = Parameters(qbits=512, rbits=160)  # type a
    pairing = Pairing(params)
    # G2 generator g
    g = Element.random(pairing, G2)  # 1024 bytes
    # x y
    x_ran = Element.random(pairing, Zr)  # 160 bits
    y_ran = Element.random(pairing, Zr)
    c1='1'+str(hash_value_x_t)
    c2 ='2'+ str(hash_value_x_t)
    hash_value_x_t = Element.from_hash(pairing, Zr,c1)
    print('11', hash_value_x_t)
    hash_value_x_t = Element.from_hash(pairing, Zr, c2)
    print('22', hash_value_x_t)

    print(hash_value_x_t)
Error:
/usr/bin/python3.9 /home/ali/Downloads/MedShare2021-main/constant_abe.py Traceback (most recent call last): File "/home/ali/Downloads/MedShare2021-main/constant_abe.py", line 232, in <module> (pk, msk) = setup() File "/home/ali/Downloads/MedShare2021-main/constant_abe.py", line 26, in setup c1='1'+str(hash_value_x_t) UnboundLocalError: local variable 'hash_value_x_t' referenced before assignmen



RE: NameError: name 'hash_value_x_t' is not defined - bowlofred - May-06-2022

The variable has to be given a value before you can examine the value.

Line 17 is trying to read the value and there isn't a value to be read.


RE: NameError: name 'hash_value_x_t' is not defined - Anldra12 - May-13-2022

Yes, you are right but c1 and c2 are two variables in code for which we basically apply hash_value_x_t for mapping to some stuff in other codes.


RE: NameError: name 'hash_value_x_t' is not defined - deanhystad - May-13-2022

What c1 or c2 do is of no importance. Getting the value of a variable which has not yet been assigned a value raises an exception. Your code must do something to fix this first use problem.