-1

this code returns the error : (local variable 'array' referenced before assignment)

def chooselist():
    return(array)
    if array==1:
        for i in range(3):
            print("The first/next number is", bingo_array[random.randint(0,9)])
            print("The first/next number is", bingo_array[random.randint(0,9)])
            print("The first/next number is", bingo_array[random.randint(0,9)])
            print(" ")
    elif array==2:
        for i in range(3):
            print("The first/next number is", bingo_array2[random.randint(0,9)])
            print("The first/next number is", bingo_array2[random.randint(0,9)])
            print("The first/next number is", bingo_array2[random.randint(0,9)])
            print(" ")
    elif array==3:
        for i in range(3):
            print("The first/next number is", bingo_array3[random.randint(0,9)])
            print("The first/next number is", bingo_array3[random.randint(0,9)])
            print("The first/next number is", bingo_array3[random.randint(0,9)])
            print(" ")
    elif array==4:
        for i in range(3):
            print("The first/next number is", bingo_array4[random.randint(0,9)])
            print("The first/next number is", bingo_array4[random.randint(0,9)])
            print("The first/next number is", bingo_array4[random.randint(0,9)])
            print(" ")
    elif array==5:
        for i in range(3):
            print("The first/next number is", bingo_array5[random.randint(0,9)])
            print("The first/next number is", bingo_array5[random.randint(0,9)])
            print("The first/next number is", bingo_array5[random.randint(0,9)])
            print(" ")
    elif array==6:
        for i in range(3):
            print("The first/next number is", bingo_array6[random.randint(0,9)])
            print("The first/next number is", bingo_array6[random.randint(0,9)])
            print("The first/next number is", bingo_array6[random.randint(0,9)])
            print(" ")
    elif array==7:
        for i in range(3):
            print("The first/next number is", bingo_array7[random.randint(0,9)])
            print("The first/next number is", bingo_array7[random.randint(0,9)])
            print("The first/next number is", bingo_array7[random.randint(0,9)])
            print(" ")
    elif array==8:
        for i in range(3):
            print("The first/next number is", bingo_array8[random.randint(0,9)])
            print("The first/next number is", bingo_array8[random.randint(0,9)])
            print("The first/next number is", bingo_array8[random.randint(0,9)])
            print(" ")
    elif array==9:
        for i in range(3):
            print("The first/next number is", bingo_array9[random.randint(0,9)])
            print("The first/next number is", bingo_array9[random.randint(0,9)])
            print("The first/next number is", bingo_array9[random.randint(0,9)])
            print(" ")
    array=array+1
    if array==9:
        done=True

for i in range(9):
    chooselist()
Maurice Meyer
  • 17,279
  • 4
  • 30
  • 47
  • 3
    Because u didn't assign any value to it? – Sid Oct 01 '19 at 11:29
  • 1
    All the code after `return(array)` is unreachable. And at that line, `array` is not defined (at least not locally) – rdas Oct 01 '19 at 11:30
  • `Line 2: return array`. You can't return a variable without initializing or assigning it to something. – Saharsh Oct 01 '19 at 11:30
  • Also lines 3, 9 etc. u haven't defined ```array```. How can you just use it? – Sid Oct 01 '19 at 11:31
  • maybe start with an explanation of what it is you actually want to achieve with the code? It seems you try to use variables in the function that are defined in the outer scope - that's hard to read and considered bad practice. – FObersteiner Oct 01 '19 at 11:37
  • Welcome to Stack Overflow! Please [edit] your code to reduce it to a [mcve] of your problem. Your current code includes much that is peripheral to your problem - a minimal sample normally looks similar to a good unit test: only performing one task, with input values specified for reproducibility. – Toby Speight Oct 01 '19 at 12:04
  • Possible duplicate of [Python variable scope error](https://stackoverflow.com/questions/370357/python-variable-scope-error) – pringi Oct 01 '19 at 13:02

2 Answers2

0

I believe this is more like what you are after. Pass "array" as an argument to the function within your for loop. You are just printing stuff within your function so no need to return anything.


def chooselist(array):
    if array==1:
        for i in range(3):
            print("The first/next number is", bingo_array[random.randint(0,9)])
            print("The first/next number is", bingo_array[random.randint(0,9)])
            print("The first/next number is", bingo_array[random.randint(0,9)])
            print(" ")
    elif array==2:
        for i in range(3):
            print("The first/next number is", bingo_array2[random.randint(0,9)])
            print("The first/next number is", bingo_array2[random.randint(0,9)])
            print("The first/next number is", bingo_array2[random.randint(0,9)])
            print(" ")
    elif array==3:
        for i in range(3):
            print("The first/next number is", bingo_array3[random.randint(0,9)])
            print("The first/next number is", bingo_array3[random.randint(0,9)])
            print("The first/next number is", bingo_array3[random.randint(0,9)])
            print(" ")
    elif array==4:
        for i in range(3):
            print("The first/next number is", bingo_array4[random.randint(0,9)])
            print("The first/next number is", bingo_array4[random.randint(0,9)])
            print("The first/next number is", bingo_array4[random.randint(0,9)])
            print(" ")
    elif array==5:
        for i in range(3):
            print("The first/next number is", bingo_array5[random.randint(0,9)])
            print("The first/next number is", bingo_array5[random.randint(0,9)])
            print("The first/next number is", bingo_array5[random.randint(0,9)])
            print(" ")
    elif array==6:
        for i in range(3):
            print("The first/next number is", bingo_array6[random.randint(0,9)])
            print("The first/next number is", bingo_array6[random.randint(0,9)])
            print("The first/next number is", bingo_array6[random.randint(0,9)])
            print(" ")
    elif array==7:
        for i in range(3):
            print("The first/next number is", bingo_array7[random.randint(0,9)])
            print("The first/next number is", bingo_array7[random.randint(0,9)])
            print("The first/next number is", bingo_array7[random.randint(0,9)])
            print(" ")
    elif array==8:
        for i in range(3):
            print("The first/next number is", bingo_array8[random.randint(0,9)])
            print("The first/next number is", bingo_array8[random.randint(0,9)])
            print("The first/next number is", bingo_array8[random.randint(0,9)])
            print(" ")
    elif array==9:
        for i in range(3):
            print("The first/next number is", bingo_array9[random.randint(0,9)])
            print("The first/next number is", bingo_array9[random.randint(0,9)])
            print("The first/next number is", bingo_array9[random.randint(0,9)])
            print(" ")
    if array==9:
        done=True

for i in range(9):
    chooselist(i)
tomgalpin
  • 1,943
  • 1
  • 4
  • 12
0

In the beginning of your code you write return array. However at no point have you assigned any value to array so Python is confused because it doesn't know what to return.

To make your code run there are a few things you should take care of:

  • Make sure your return statement is at the right place inside your function (most likely at the end)
  • Make sure you initialize array somewhere before you start using it
  • Change all the bingo_array_x (I actually think you're not going to need them anyway)

I'm not entirely sure, but I think you want to make something similar to:

import numpy as np

for i in range(9):
        draw = np.random.randint(0, 9, 3)
        print('draw #{}: {}'.format(i + 1, draw))

Maybe this will help you. Good luck!