Python if 和 else 语句不是预期的输出

Python if and else statement not expected output

我正在尝试使用 python 3.9 进行动物测验。在其中一个问题上错了 3 次并没有像它应该的那样开始一个新问题。相反,它只是空白。在这个人的第一次和第二次尝试中,一切都很顺利。任何和所有的帮助表示赞赏。这是我的代码:

def check_guess(guess, answer):
    global score
    global name
    still_guessing = True
    attempt = 0
    while still_guessing and attempt < 3:
        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
        elif attempt <= 1:
            print('HAAAAAAAAAAAAAAAAAAAAAAAAAAAA IMAGINE NOT GETTING THAT RIGHT!!!')
            print('L bozo + ratio')
            guess = input('Try again ')
            attempt = attempt + 1
    if attempt == 3:
        print('the correct answer was %s' % answer)
        print('There is always next time!!')
        still_guessing = False
score = 0
print('Welcome to the animal quiz')
print()
print('In this game you will have to guess the animal')
name = input('What is your name? ')
print('Cool name, I feel like i heard of it before hmmm %s..' % name)
print()
print('Enough stalling onto the game!!!')
guess1 = input('Which bear lives in the north pole ')
check_guess(guess1.strip().lower(), 'polar bear')

你必须把 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

我认为下面的 elif 应该计算 <= 2,而不是 1:

        elif attempt <= 2:

但最后一条 'Try again' 消息仍然打印出来。您可以解决将尝试检查条件放在 'Try again' 消息之前的问题。如果条件评估为 True,则中断循环:

        elif attempt <= 2:
            print('HAAAAAAAAAAAAAAAAAAAAAAAAAAAA IMAGINE NOT GETTING THAT RIGHT!!!')
            print('L bozo + ratio')
            attempt = attempt + 1
            if attempt > 2:
                break            
            guess = input('Try again ')

请记住在这种情况下调整您的 While 条件,因为不再需要尝试检查。

如果可以的话,我对代码进行了一些重构,因此您可以查看实现相同目标的其他方法。

def check_guess(guess, answer, attempts):
    global score
    global name
    while True:
        if guess == answer:
            print('Correct wow, i expected worse from someone named %s' % name)
            score = [0, 1, 2, 3]
            final_score = score[attempts]
            print(f'Score: {final_score}')
            break

        attempts -= 1

        if attempts == 0:
            print('the correct answer was %s' % answer)
            print('There is always next time!!')
            break
        
        print('HAAAAAAAAAAAAAAAAAAAAAAAAAAAA IMAGINE NOT GETTING THAT RIGHT!!!')
        print('L bozo + ratio')
        guess = input('Try again ')

score = 0
print('Welcome to the animal quiz')
print()
print('In this game you will have to guess the animal')

name = input('What is your name? ')
print('Cool name, I feel like i heard of it before hmmm %s..' % name)
print()
print('Enough stalling onto the game!!!')

guess1 = input('Which bear lives in the north pole ')
check_guess(guess1.strip().lower(), 'polar bear', attempts=3)

使用下面的代码对我有用。

def check_guess(guess, answer):
    global score
    global name
    still_guessing = True
    attempt = 0
    while still_guessing and attempt < 3:
        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
        elif attempt <= 2:
            print('HAAAAAAAAAAAAAAAAAAAAAAAAAAAA IMAGINE NOT GETTING THAT RIGHT!!!')
            print('L bozo + ratio')
            guess = input('Try again ')
            attempt = attempt + 1
    if attempt == 3:
        print('the correct answer was %s' % answer)
        print('There is always next time!!')
        still_guessing = False
score = 0
print('Welcome to the animal quiz')
print()
print('In this game you will have to guess the animal')
name = input('What is your name? ')
print('Cool name, I feel like i heard of it before hmmm %s..' % name)
print()
print('Enough stalling onto the game!!!')
guess1 = input('Which bear lives in the north pole ')
check_guess(guess1.strip().lower(), 'polar bear')

我把1改成2了

在你的 while 循环中 'attempt'永远不会变成3,所以代码无法跳转到下一部分if attmpt == 3 所以在while循环中,elif条件应该是elif attempt <= 2:那么attempt = attempt + 1可以达到3

我发现了一些错误。 首先,您不需要 if attempt == 3 或 2 因为您使用的是 while 循环(while 循环首先基于条件)。 第二,最好结合“break”,不要死循环。 While 循环从 0 开始到 2 结束(取 3 个值)。

我为你重写了代码。

def check_guess(guess, answer):
global score
global name
still_guessing = True
attempt = 0
while still_guessing and attempt < 2:
    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
    elif attempt <= 1:
        print('HAAAAAAAAAAAAAAAAAAAAAAAAAAAA IMAGINE NOT GETTING THAT RIGHT!!!')
        print('L bozo + ratio')
        guess = input('Try again ')
        attempt = attempt + 1
    break

print('the correct answer was %s' % answer)
print('There is always next time!!')
still_guessing = False

要测试代码,请添加 print("pass OK")

score = 0
print('Welcome to the animal quiz')
print()
print('In this game you will have to guess the animal')
name = input('What is your name? ')
print('Cool name, I feel like i heard of it before hmmm %s..' % name)
print()
print('Enough stalling onto the game!!!')
guess1 = input('Which bear lives in the north pole ')
check_guess(guess1.strip().lower(), 'polar bear')
print("pass OK")

别忘了投票 :)(斋月卡里姆!!!)