循环中的Python if-else不起作用(跳过if)

时间:2015-04-24 19:30:21

标签: python-2.7

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)

为什么最后一个if-else跳过'if'然后无限地打印not found

1 个答案:

答案 0 :(得分:0)

你应该有一个状态变量,如# 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"

1, 2, 3, 4, 5, ...