How can I repeat inputting with the for loop numbering if the answer is not accepted?

Viewed 41

I am using a for loop for numbering for my input answers. If the user gets a correct answer, it will stored in the list. But, if the user want to repeat the answer, it will print out that the user repeated the answer but the problem is, instead of inputting in the same number, it will go on the next number

answer = []
while lives > 0:
    while repeat:
        for i in range(0,10):
            answerlst = input(str(i + 1) + '. ').upper()
            if answerlst in text:
                if answerlst in answer:
                    print("You repeated your answer!")
                    repeat = True
                else:
                    answer.append(answerlst)
                    repeat = False
            else:
                print("Incorrect. You just lose a life")
                lives -= 1
                if lives == 0:
                    print("GAME OVER!!!!!\n")
                    break

        ask = input("\nWanna play again? [Y] / [N]: ").upper()
        if ask == 'Y':
            tryAgain = True
        else:
            tryAgain = False

Example:

  1. Dark
  2. Dark You repeated your answer!
  3. #This is the problem

Instead of inputting in number 3, it should be number 2 because it only repeats the answer.

1 Answers

In such cases using a while loop is the best choice. Assign a variable as starting point and then increment if condition false, if true use continue then it would have the same index.

answer = []
while lives > 0:
    while repeat:
        index = 0
        while index < 10:
            answerlst = input(str(index + 1) + '. ').upper()
            if answerlst in text:
                if answerlst in answer:
                    print("You repeated your answer!")
                    repeat = True
                    continue
                else:
                    answer.append(answerlst)
                    repeat = False
            else:
                print("Incorrect. You just lose a life")
                lives -= 1
                if lives == 0:
                    print("GAME OVER!!!!!\n")
                    break
            index += 1
        ask = input("\nWanna play again? [Y] / [N]: ").upper()
        if ask == 'Y':
            tryAgain = True
        else:
            tryAgain = False
Related