[Python] UnboundLocalError: cannot access local variable 'have_exceptions_n' where it is not...

Discussão em 'Python' iniciado por Stack, Março 29, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to program a game and when I was checking edge cases (in the same function), it gives me a UnboundLocalError:


    UnboundLocalError: cannot access local variable 'have_exceptions_n' where it is not associated with a value
    ​

    The code:

    def get_input():
    while True:
    have_exceptions = False
    player_nums = input('\nEnter 5 numbers between 1 and 69, seperate with spaces (e.g. 1 2 3 4 5):\n> '); player_nums = player_nums.split()
    for i in range(len(player_nums)):
    if not player_nums.isnumeric():
    have_exceptions_n = True
    have_exceptions = True
    elif int(player_nums) > 69:
    have_exceptions_n = True
    have_exceptions = True
    player_nums.append(input('Enter the POWERBALL NUMBER between 1 and 26 (e.g. 14):\n> '))
    if not player_nums[5].isnumeric():
    have_exceptions_p = True
    have_exceptions = True
    elif int(player_nums[5]) > 26:
    have_exceptions_p = True
    have_exceptions = True
    number_of_sim = input('How many times should I simulate this? (max 10000):\n> ')
    if not number_of_sim.isnumeric():
    have_exceptions_s = True
    have_exceptions = True
    elif int(number_of_sim) > 10000:
    have_exceptions_s = True
    have_exceptions = True
    if not have_exceptions:
    break # inputs past all the tests, break out the loop.
    if have_exceptions_n:
    player_nums[:5] = input('One of your given lottery numbers are not valid. Enter numbers that follow the rules (seperate with spaces):\n> ').split()
    elif have_exceptions_p:
    player_nums[5] = input('Your POWERBALL NUMBER is not valid. Enter something like this: 24\n> ')
    elif have_exceptions_s:
    number_of_sim = input('Your given simulaiton number is not a number. Enter something like this: 7809\n> ')
    for i in range(len(player_nums)):
    player_nums = int(player_nums)
    number_of_sim = int(number_of_sim)
    return player_nums, number_of_sim


    Output:

    Enter 5 numbers between 1 and 69, seperate with spaces (e.g. 1 2 3 4 5):
    6 3 45 2 13
    Enter the POWERBALL NUMBER between 1 and 26 (e.g. 14):
    2
    How many times should I simulate this (max 10000):
    67
    Traceback (most recent call last):
    File "d:\Programs\Powerball_Lottery", line 103, in <module>
    main()
    ~~~~^^
    File "d:\Programs\Powerball_Lottery", line 77, in main
    player_nums, number_of_sim = get_input()
    ~~~~~~~~~^^
    File "d:\Programs\Powerball_Lottery", line 47, in get_input
    if have_exceptions_n:
    ^^^^^^^^^^^^^^^^^
    UnboundLocalError: cannot access local variable 'have_exceptions_n' where it is not associated with a value


    I have tried to put global have_exceptions_n on the next line of def get_input(): and it give me an NameError: 'have_exceptions_n' was not defined and then, I searched on the internet but it didn't help. Is there any ways I can get this program run properly?

    Continue reading...

Compartilhe esta Página