-1

I have 2 functions: check_question_count() that asks a user to give an input of a number between 1 and 15 , validates the input, then returns it; random_question() function takes that returned number and generates a number of questions.

The issue is that the returned value from the first function does not seem to be returned, because i get an error that says: UnboundLocalError: local variable 'q_count' referenced before assignment.

def random_question():
    check_question_count()
    q_index = 1
    global saved_question_count
    saved_question_count = int(q_count)
    print('Awesome! you will be asked ', saved_question_count,' randomly selected questions out of the total of 15 questions available.')
    #Using a while loop to print out a randomly selected question:
    while q_count != 0:
       b = len(questions_list) - 1
       rand_num = random.randint(0,b)
       global selected_question
       selected_question = questions_list[rand_num]
       print('\n')
       print(q_index,'.',selected_question)
       global user_answer
       user_answer = input('Your answer is: ').capitalize()
       #Using a function to validate the user_answer input:
       check_submitted_answers(user_answer)
       questions_list.remove(selected_question)
       questions_list =  questions_list
       q_count = q_count - 1
       q_index = q_index +1
           

#4- Function to check the question count input and make sure its a number between 1 to 15:
def check_question_count():
        global q_count
        q_count = input('\nHow many questions do you like to take? You can choose a number between 1 to 15: ')
        while True:
          if q_count not in ['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15']:
            print('Error: Invalid input! Please type in only a number between 1 and 15: ')
            q_count = input('\nHow many questions do you like to take? You can choose a number between 1 to 15: ')
            continue
          else:
            return q_count

random_question()

Any help is appreciated, thanks.

Nishant
  • 20,354
  • 18
  • 69
  • 101
  • `q_count = check_question_count()` in `random_question` – Matiiss Jul 27 '22 at 11:17
  • 1
    Compare the line where you call `check_question_count` with other lines where you call functions and use their return values. What's different? – mkrieger1 Jul 27 '22 at 11:17
  • when you call a function, if you want to make use of the return value you have to assign the result of the call to a variable e.g. `myvar= check_question_count()` ... e.g. here you could name it q_count `q_count = check_question_count()` ... but note that this is a different `q_count` variable from the one inside the other function (even though in this case it will have the same value, since we are returning one into the other) – Anentropic Jul 27 '22 at 11:17

2 Answers2

1

There are two problems with your code. One is severe, the other is not.

The root cause of the error UnboundLocalError: local variable 'q_count' referenced before assignment is the line q_count = q_count - 1 in random_question(). This supposed to modify the variable, but that variable is supposed to be global, and thus, not writable. This makes python believe you have a local, modifiable variable q_count, and thus, all the mentions of it in the current scope must mean this local one - and when you reference it first, it is referenced before assigned.

One solution would be to just use the global version, i.e. global saved_question_count, q_count. Another one would be to remove the line q_count = q_count - 1 - but you probably need that line. A third one would be to create a new (local) variable, and initialize it with q_count.

The best solution would be (that also fixes the second problem of unnecessary global variables) is to just return the value. Others have already explained that in detail.

Uncle Dino
  • 812
  • 1
  • 7
  • 23
0

You are not saving the returned value in any given variable i.e instead of

check_question_count()

try:

q_count = check_question_count()

in order to save the returned value in a variable.

You should also look into the proper use of global keyword because it is being used unnecessarily here-Use of "global" keyword

  • The poster already seems to have saved the result into a global variable. I agree that returning it and not using `global` is better, but the cause of the problem is not adressed in this answer. – Uncle Dino Jul 27 '22 at 11:20
  • @Sasszem Yes, they have created a global variable but that is not how a global variable is used. By saving the value in a variable and removing global, the error will go away. – Purusharth Malik Jul 27 '22 at 11:22