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.