Python 2.7 循环中的Python if-else不工作(if跳过)

Python 2.7 循环中的Python if-else不工作(if跳过),python-2.7,Python 2.7,为什么最后一个if-else跳过“if”,然后无限打印not found?您应该有一个类似found的状态变量 它真的是无限打印吗?或者sheet.nrows times sheet.ncols只是一个非常大的数字?请记住,对于值不等于city的每个单元格,“未找到”将打印一次。如果您只希望找不到打印一次,则它不属于循环内部。它将跳过If块,因为cell.value!=推测为city。即使cell.value==city,它也会跳过if块。是的,我只希望它打印一次未找到,当且仅当没有单元格==c

为什么最后一个if-else跳过“if”,然后无限打印not found?

您应该有一个类似found的状态变量


它真的是无限打印吗?或者sheet.nrows times sheet.ncols只是一个非常大的数字?请记住,对于值不等于city的每个单元格,“未找到”将打印一次。如果您只希望找不到打印一次,则它不属于循环内部。它将跳过If块,因为cell.value!=推测为city。即使cell.value==city,它也会跳过if块。是的,我只希望它打印一次未找到,当且仅当没有单元格==cit,是的,sheet.nrows times sheet.ncols是一个相当大的数字。大约20万
def searcher():
    mun = raw_input("Enter name of city> ")
    pal = mun.replace(' ', '')
    state = raw_input("Enter State abbreviation> ")
    city = pal + state
    city = city.upper()
    for r in range(sheet.nrows):
        for c in range(sheet.ncols):
            cell = sheet.cell(r, c)
            if cell.value == city:
                loc = r
            else:
                print "not found"
    cell = sheet.cell(loc, 15)
# Start out False
found = False
for r in range(sheet.nrows):
    for c in range(sheet.ncols):
        cell = sheet.cell(r, c)
        if cell.value == city:
            loc = r
            found = True # set it to True if we find the value
# Now, the for loops are done.
if not found:
    print "not found"