私信  •  关注

rushBblyat

rushBblyat 最近创建的主题
rushBblyat 最近回复了
3 年前
回复了 rushBblyat 创建的主题 » Python if and else语句不是预期的输出

你必须把 if attempt == 3 在你的while循环中,因为while循环无限地运行,直到猜测是正确的,所以当 attempt 的值是3,它将什么也不做,因为它仍然在一个循环中,并且没有if语句告诉它值为3后该做什么。

编辑:还将循环条件更改为 still guessing and attempt <= 3

attempt = 0
def check_guess(guess, answer):
    global score
    global name
    global attempt
    still_guessing = True

    while still_guessing and attempt <= 3:
        print(f"attempt {attempt}")
        if attempt == 2:
            print('the correct answer was %s' % answer)
            print('There is always next time!!')
            still_guessing = False
    
        else:
            print('HAAAAAAAAAAAAAAAAAAAAAAAAAAAA IMAGINE NOT GETTING THAT RIGHT!!!')
            print('L bozo + ratio')
            guess = input('Try again ')
            attempt = attempt + 1

        if guess == answer:
            print('Correct wow, i expected worse from someone named     %s' % name)
            if attempt == 0:
                score = score + 3
            elif attempt == 1:
                score = score + 2
            elif attempt == 2:
                score = score + 1
            still_guessing = False