Python if and else语句不是预期输出



我正试图用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:

但是,上一条"重试"消息仍然打印出来。您可以解决将尝试检查条件放在"重试"消息之前的问题。如果条件评估为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)

我注意到一些错误。首先,您不需要尝试==3或2,因为您使用的是while循环(while循环首先基于条件(。第二,最好将";中断";不要有一个无限循环。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")

别忘了投票支持:((Ramadan Karim!!(

在while循环中"尝试"永远不会变成3,因此代码无法跳转到下一部分if attmpt == 3所以在while循环中,elif条件应该是elif attempt <= 2:,那么attempt = attempt + 1可以达到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。

相关内容

  • 没有找到相关文章

最新更新