【发布时间】:2018-09-12 21:53:14
【问题描述】:
假设以下最小代码:
x = input("Numeral: ")
y = input("Numeral: ")
if x < y:
print(f'{x} is less than {y}.')
if x > y:
print(f'{x} is greater than {y}.')
else:
print(f'{x} is equal to {y}.')
我喂它然后过来
$ python3 draft.py
Numeral: 1
Numeral: 0
1 is greater than 0.
运行正常,然后将顺序改为输入:
$ python3 draft.py
Numeral: 0
Numeral: 1
0 is less than 1.
0 is equal to 1.
else 分支已执行。
else 分支是否只与最近的“if”配对?
其背后的机制是什么?
【问题讨论】:
-
Does else branch only pair to its nearest 'if'?是的。将第二个if更改为elif即可。 -
你今天已经向人们询问过这个问题stackoverflow.com/questions/49627409/…。为什么不编辑您的问题?
标签: python