Python Forum
NameError: name 'hash_value_x_t' is not defined
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
NameError: name 'hash_value_x_t' is not defined
#1
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
Reply
#2
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
Reply
#3
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
Reply
#4
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.
Reply
#5
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.
Reply
#6
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I'm getting a NameError: ...not defined. vonArre 2 261 Mar-24-2024, 10:25 PM
Last Post: vonArre
  Getting NameError for a function that is defined JonWayn 2 1,095 Dec-11-2022, 01:53 PM
Last Post: JonWayn
Question Help with function - encryption - messages - NameError: name 'message' is not defined MrKnd94 4 2,876 Nov-11-2022, 09:03 PM
Last Post: deanhystad
  [split] NameError: name 'csvwriter' is not defined. Did you mean: 'writer'? cathy12 4 3,302 Sep-01-2022, 07:41 PM
Last Post: deanhystad
  NameError: name ‘app_ctrl’ is not defined 3lnyn0 0 1,507 Jul-04-2022, 08:08 PM
Last Post: 3lnyn0
  NameError: name 'cross_validation' is not defined tmhsa 6 13,335 Jan-17-2022, 09:53 PM
Last Post: TropicalHeat
  NameError: name “x” is not defined ... even though x is defined campjaybellson 7 14,941 Oct-20-2021, 05:39 PM
Last Post: deanhystad
  NameError: name 'Particle' is not defined in Pygame drunkenneo 4 3,371 Aug-15-2021, 06:12 PM
Last Post: bowlofred
  NameError: name 'u1' is not defined (on parser code Python) Melcu54 1 2,873 Jul-26-2021, 04:36 PM
Last Post: snippsat
  I am getting a NameError that is not defined and not sure why it happen rick0922 5 4,081 Jun-14-2021, 03:41 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020