Python 没有跳入 "else" 条件
Python not jumping into "else" condition
早上好,
这是我第一个 post 在这里 & 我只是在编程池中轻拍我的脚所以请温柔点:)
按照目前的情况,我必须满足 2 个条件(sadcat & great == yes)。我想要的是直接跳到其他('Why did you even start this game in the first place? This is a rhetorical question btw.'),如果第一个条件不满足(sadcat == 'yes' 以外的东西)
我不知道为什么这行不通......缩进对我来说似乎没问题。感谢任何帮助
sadcat = input('Do you like war? ')
great = input ('That\'s great but are you also a cat? ')
if sadcat == 'yes':
if great == 'yes':
print('ok I have no more questions')
else:
print('Not a cat ? How do you even live with yourself.')
else:
print('Why did you even start this game in the first place? This is a rhetorical question btw.', end=" ")
如果你想让“游戏”在“玩家”不喜欢的情况下结束war,那么第一个if应该紧跟在第一个问题之后
sadcat = input('Do you like war? ')
if sadcat == 'no':
print('Why did you even start this game in the first place? This is a rhetorical question btw.', end=" ")
exit()
great = input ('That\'s great but are you also a cat? ')
if great == 'yes':
print('ok I have no more questions')
else:
print('Not a cat ? How do you even live with yourself.')
所以基本上代码会按照编写的顺序执行每一行。
在您的代码中,它首先接受 sadcat 的输入,然后输入 great。
仅在此之后它才开始评估 sadcat。
(你的代码是正确的。一旦它到达评估部分,它将直接进入else)
如果您希望游戏在玩家输入 'yes'
以外的任何内容后立即结束
import sys
sadcat = input('Do you like war? ')
if sadcat != 'yes':
print('Why did you even start this game in the first place? This is a rhetorical question btw.', end=" ")
sys.exit() #this will terminate the program
great = input ('That\'s great but are you also a cat? ')
if sadcat == 'yes':
if great == 'yes':
print('ok I have no more questions')
else:
print('Not a cat ? How do you even live with yourself.')
早上好,
这是我第一个 post 在这里 & 我只是在编程池中轻拍我的脚所以请温柔点:)
按照目前的情况,我必须满足 2 个条件(sadcat & great == yes)。我想要的是直接跳到其他('Why did you even start this game in the first place? This is a rhetorical question btw.'),如果第一个条件不满足(sadcat == 'yes' 以外的东西) 我不知道为什么这行不通......缩进对我来说似乎没问题。感谢任何帮助
sadcat = input('Do you like war? ')
great = input ('That\'s great but are you also a cat? ')
if sadcat == 'yes':
if great == 'yes':
print('ok I have no more questions')
else:
print('Not a cat ? How do you even live with yourself.')
else:
print('Why did you even start this game in the first place? This is a rhetorical question btw.', end=" ")
如果你想让“游戏”在“玩家”不喜欢的情况下结束war,那么第一个if应该紧跟在第一个问题之后
sadcat = input('Do you like war? ')
if sadcat == 'no':
print('Why did you even start this game in the first place? This is a rhetorical question btw.', end=" ")
exit()
great = input ('That\'s great but are you also a cat? ')
if great == 'yes':
print('ok I have no more questions')
else:
print('Not a cat ? How do you even live with yourself.')
所以基本上代码会按照编写的顺序执行每一行。 在您的代码中,它首先接受 sadcat 的输入,然后输入 great。
仅在此之后它才开始评估 sadcat。 (你的代码是正确的。一旦它到达评估部分,它将直接进入else)
如果您希望游戏在玩家输入 'yes'
以外的任何内容后立即结束import sys
sadcat = input('Do you like war? ')
if sadcat != 'yes':
print('Why did you even start this game in the first place? This is a rhetorical question btw.', end=" ")
sys.exit() #this will terminate the program
great = input ('That\'s great but are you also a cat? ')
if sadcat == 'yes':
if great == 'yes':
print('ok I have no more questions')
else:
print('Not a cat ? How do you even live with yourself.')