Search code examples
pythonreturntry-catchbreak

Why am I not breaking out of a try loop with a break statement?


My code for now works as desired where the user can input a level 1-3 depending on how hard they would like it to be (1-3 being the amount of digits the numbers will have in the math equation), and then must solve math equations. Those math equations will output EEE if the answer is incorrect and everything works as planned if you correctly answer the question as it exits the function and adds one total_correct_answers variable at the bottom, then will prompt you with another equation. However, if you input an incorrect answer and then a correct answer, you will just be prompted with the same question over and over again without the try loop being truly broken out of and total_correct_answers not being incremented positively by 1. The incrementation block of code is at lines 61-65, and the equation code is lines 30-49.

import random

def main():
    ten_questions()

def get_level():
    while True:
        try:
            level_input = int(input("Level: "))
            if level_input in [1,2,3]:
                return level_input
        except:
            pass


def integer_generator(level):
    if level == 1:
        x = random.randint(0,9)
        y = random.randint(0,9)
    elif level == 2:
        x = random.randint(10, 99)
        y = random.randint(10, 99)
    else:
        x = random.randint(100, 999)
        y = random.randint(100, 999)
    return x, y



def question_generator(x, y):
    real_answer = x + y
    wrong_counter = 0
    while True:
        try:
            answer_given =  input(str(x) + " + " + str(y) + " = ")
            if int(answer_given) == real_answer:
                if wrong_counter == 0:
                    return True
            elif int(answer_given) == real_answer and wrong_counter != 0:
                break
            else:
                while wrong_counter < 2:
                    print("EEE")
                    wrong_counter +=1
                    break
                else:
                    print(str(x) + " + " + str(y) + " = " + str(real_answer))
                    print("False, that was last attempt")
                    break

        except:
            print("EEE")
            pass


def ten_questions():
    num_of_questions = 0
    total_correct_answers = 1
    my_level = get_level()
    correct_answers = question_generator(*integer_generator(my_level))
    while num_of_questions <= 8:
        question_generator(*integer_generator(my_level))
        num_of_questions +=1
        if correct_answers == True:
            total_correct_answers +=1
    print("Score: " + str(total_correct_answers))




if __name__ == "__main__":
    main()

Solution

  • Because of your line 36:

    if int(answer_given) == real_answer: happens when someone answers correctly, wether they are right or wrong. So it enters the if, and then faces if wrong_counter == 0: which discards wrong answers. So just replace those two lines with if int(answer_given) == real_answer and wrong_counter == 0: and you are good to go.