我认为
elif
贝娄应该评估<=2,而不是1:
elif attempt <= 2:
但最后一条“再试一次”的信息仍然打印出来。您可以在“重试”消息之前设置一个尝试检查条件来解决这个问题。如果条件求值为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 ')
在这种情况下,请记住调整您的状态,因为不再需要尝试检查。
如果可以的话,我在代码中做了一些重构,因此您可以查看其他方法来实现相同的目标。
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)