我只是在学习python,很难理解if输入触发else语句的原因。我肯定我错过了一些基本的东西,但希望有人看看它!本质上,当我输入一个变量时,它会将else语句拖到其中。我附上代码,谢谢看!你知道吗
n = 'Nike'
p = 'Puma'
a = 'Adidas'
boot = input('What is your favorite boot?')
if boot == n:
print('Nike, great choice')
if boot == a:
print('Adidas, not my favorite')
if boot == p:
print('Not sure about Puma')
else:
print('I am not familiar with that brand')
在输入打印上键入Nike
Nike, great choice.
I'm not familiar with that brand.
当前输入的内容是“您最喜欢的靴子是什么?” 我会在输入之前打印提示,就像下面的代码一样
现在else只与最后一个if语句相关 尝试使用Elif语句将它们联系在一起(如下)
输出:
如果
boot
等于n
,会发生什么?从上到下执行,并执行所有测试:boot == n
。印刷的。你知道吗boot != a
,没有打印。你知道吗boot != p
,否则执行部分。你知道吗如果匹配,为了抑制进一步的测试,请使用
elif
:您在这里创建了三个独立的if语句。看一下附带的带括号的伪代码
您需要使用“elif”:
相关问题 更多 >
编程相关推荐