简体   繁体   English

为什么我不使用 break 语句跳出 try 循环?

[英]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.我的代码现在可以按需要工作,用户可以根据他们想要的难度输入 1-3 级(1-3 是数学方程中数字的位数),然后必须解决数学方程。 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.如果答案不正确,则这些数学方程式将为 output EEE,并且如果您正确回答问题,因为它退出 function 并在底部添加一个 total_correct_answers 变量,然后将提示您使用另一个方程式。 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.但是,如果您输入了一个错误的答案,然后又输入了一个正确的答案,您只会被一遍又一遍地提示相同的问题,而不会真正打破 try 循环,并且 total_correct_answers 不会正向递增 1。代码的递增块在第 61-65 行,方程式代码在第 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()

Because of your line 36:由于您的第 36 行:

if int(answer_given) == real_answer: happens when someone answers correctly, wether they are right or wrong. if int(answer_given) == real_answer:当有人正确回答时发生,无论他们是对还是错。 So it enters the if, and then faces if wrong_counter == 0: which discards wrong answers.所以它进入 if,然后面对if wrong_counter == 0:丢弃错误的答案。 So just replace those two lines with if int(answer_given) == real_answer and wrong_counter == 0: and you are good to go.因此,只需将这两行替换为if int(answer_given) == real_answer and wrong_counter == 0:就可以了 go。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM