0

I have already tried many times to fix this problem, but I can not solve it in any way. I use an index as a variable to move the user to the next question, but after correct and incorrect answers, the index remains at 0.

score = 0
index = 0
num = 0
user_answer = ""
correct = ""

#------------------------------------------------------------------------------

question_list = ["What is this shape? \na. Square \nb. Triangle \nc. Oval \nAnswer: ", "What color is this shape? \na. Blue \nb. Red \nc. Orange \nAnswer: "]
answer_list = ["a", "c"]

def AnswerCheck (num):
  global user_answer, correct, index, score
  while (correct == "false"):
    user_answer = input(question_list[num])
    if (user_answer == answer_list[num]):
      score += 1
      print("Correct!")
      print("Score:", score)
      print("\n")
    else:
      print("Incorrect!")
      print("Score:", score)
      print("\n")
    index = index + 1

while index < len(question_list):
    correct = "false"
    AnswerCheck(index)
Vlad
  • 3
  • 2

2 Answers2

1

So the reason why the index isn't moving is that you are not letting it execute. first things first, you need to define the variables outside of the functions then make a function called main. main will be the driver function that you will be using to execute the rest of the code. the next step is to place the index += 1 outside of the while loop, then and a return call at the end of the function to push the results back to main. finally, you need to stop the while loop that is asking the question, to do this add break. this will stop the while loop from executing if the correct answer is entered.

question_list = ["What is this shape? \na. Square \nb. Triangle \nc. Oval \nAnswer: ", "What color is this shape? \na. Blue \nb. Red \nc. Orange \nAnswer: "]
answer_list = ["a", "c"]
user_answer = ""
correct = "false"
index = 0
score = 0

def AnswerCheck (num):
    global index, score, user_answer, correct
    while (correct == "false"):
        user_answer = input(question_list[num])
        if (user_answer == answer_list[num]):
            score += 1
            print("Correct!")
            print("Score:", score)
            print("\n")
            break
        else:
            print("Incorrect!")
            print("Score:", score)
            print("\n")
    index = index + 1
    return
    
def main():
    while index < len(question_list):
        correct = "false"
        AnswerCheck(index)
main()

This code ran fine on my side let me know if you have any questions.

0

You have this condition while (correct == "false"), correct variable has the value "false" from the start, and you don't change correct value anywhere in the body of your loop. Hence you will never exit the loop.

And about index remaining always zero. It's not true. You can print the value of index before user_answer = input(question_list[num]) and you will see that it has changed.

The problem is that you are passing the index as an argument here AnswerCheck(index). Because int is a primitive type in Python the value of the index variable will be copied and stored in the num variable inside your AnswerCheck function.

Because of that, num in AnswerCheck and index from the outside scope are not connected in any way. That means changing index will not change the value of num and vice versa.

And you will never exit AnswerCheck as I mentioned in the beginning, so your num variable will never change, and you stuck with the num being 0.

There are a lot of ways to solve it. The most simple ones are changing your while condition or updating the num variable, depending on the desired results.

Also, consider avoiding using global it's not the best practice and can lead to some difficulties debugging. You can read more reasoning about it.

Valerii Boldakov
  • 1,751
  • 9
  • 19