Mam*_*ury 2 python if-statement
我只是在学习 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')
Run Code Online (Sandbox Code Playgroud)
在Nike输入打印上打字
Nike, great choice.
I'm not familiar with that brand.
Run Code Online (Sandbox Code Playgroud)
那么,会发生什么,例如如果boot等于n?执行从上到下并进行所有测试:
if boot == n:
print('Nike, great choice')
Run Code Online (Sandbox Code Playgroud)
boot == n. 打印。
if boot == a:
print('Adidas, not my favorite')
Run Code Online (Sandbox Code Playgroud)
boot != a,没有打印。
if boot == p:
print('Not sure about Puma')
else:
print('I am not familiar with that brand')
Run Code Online (Sandbox Code Playgroud)
boot != p, else 部分执行。
为了在匹配时抑制进一步的测试,请使用elif:
if boot == n:
print('Nike, great choice')
elif boot == a:
print('Adidas, not my favorite')
elif boot == p:
print('Not sure about Puma')
else:
print('I am not familiar with that brand')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
262 次 |
| 最近记录: |