【发布时间】:2021-07-19 10:13:07
【问题描述】:
我正在做一个项目,用户输入一个数字和一个列表,列表中最接近数字的任何项目都会被打印出来。我遇到了一个问题,我的 while 循环中的 if() 语句中的行每次都在运行吗?我觉得这与 python 3 中的 if() 语句缩进有关,但我不确定。有人知道为什么会这样吗?
import math
MatchingI = math.inf
while i < len(compareList):
if (abs(int(mainNum) - int(compareList[i])) < MatchingI):
MatchingI = int(compareList[i])
i += 1
【问题讨论】:
标签: python python-3.x if-statement while-loop