为什么这个有效:
lis = [8, 5, 6]
def fun():
for i in lis:
print i
for i in lis:
print i
fun()
而这个不是在使用csv文件的时候:
def miladifromhijri(intinp1):#, inp2, intinp3):
fulyear = intinp1 - 1
for row in majmouaread:
introw = int(row[0])
if introw <= fulyear:
years = []
years.append(introw)
near = years[::-1][0]
nearlessyear = near
for row in majmouaread:#this one doesn't get executed
print row
我想使用第二个循环使用nearlessyear变量,因为如果我继续在第一个循环中变量没有得到常量,它会继续使用在下一步中没用的数据。
答案 0 :(得分:0)
我猜你正在寻找休息声明。如果找到了您要查找的值,只需退出循环:
def miladifromhijri(intinp1):#, inp2, intinp3):
fulyear = intinp1 - 1
nearlessyear = 0
for row in majmouaread:
introw = int(row[0])
if introw <= fulyear:
years = []
years.append(introw)
near = years[::-1][0]
nearlessyear = near
break
for row in majmouaread:#this one doesn't get executed
print row