如何尝试:除了:工作而不是无限地进行
How to get try: except: working without going on infinitely
所以我有一个二维列表,名为 tour:
tour = [ [3, 2, 1,],
[4, 7, 9,],
[5, 6, 8,], ]
我想让我的代码检查这个列表,看看国王(国际象棋)是否可以正常通过它。
我编写的代码可以适用于 N×N 二维列表。它所做的是从数字 1 开始,检查它周围的所有 9 个空格是否有数字 2,重复该过程直到 N×N。问题是,当数字位于列表的一侧时,代码会给出一个 IndexError,为了克服这个问题,我做了一个 try: except: IndexError
但现在当它到达 i = 6
时,它会继续无限地 [=14] =]
def in_list(number, listname):
"""
this function detects where a certain number is in a 2d list and returns the index of
it
"""
for i, sublist in enumerate(listname):
if (number in sublist):
return [i, sublist.index(number)]
return -1
def is_king_tour_3by3(tour):
"""
this function checks for all 9 areas around this number and if it's an IndexError
i.e. it's at the end of the list it ignores that case
"""
start = in_list(1, tour)
i = 2
counter = len(tour) * len(tour)
while True:
counter -= 1
print(i)
print(start)
try:
if(tour[start[0]][start[1] - 1] == i): #starting from checking the number on the left and going counter-clockwise
i += 1
start[1] -= 1
elif(tour[start[0] + 1][start[1] - 1] == i):
i += 1
start[0] += 1
start[1] -= 1
elif(tour[start[0] + 1][start[1]] == i):
i += 1
start[0] += 1
elif(tour[start[0] + 1][start[1] + 1] == i):
i += 1
start[0] += 1
start[1] += 1
elif(tour[start[0]][start[1] + 1] == i):
i += 1
start[1] += 1
elif(tour[start[0] - 1][start[1] + 1] == i):
i += 1
start[0] -= 1
start[1] += 1
elif(tour[start[0] - 1][start[1]] == i):
i += 1
start[0] -= 1
elif(tour[start[0] - 1][start[1] - 1] == i):
i += 1
start[0] -= 1
start[1] -= 1
except IndexError:
pass
if(i == (len(tour) * len(tour))):
return True
break
elif(counter == 0):
return False
break
通过将 start[0] < len(tour)-1 and start[1] > 0
添加到行中,您可以检查您是否不在二维列表之外,而不是使用 try: except:
。只要 start is > 0 and < len(tour)
(if start[0] or start[1] is -1 or len(tour)
it might fail),此代码将解决它:
def in_list(number, listname):
"""
this function detects where a certain number is in a 2d list and returns the index of
it
"""
for i, sublist in enumerate(listname):
if (number in sublist):
return [i, sublist.index(number)]
return -1
def is_king_tour_3by3(tour):
"""
this function checks for all 9 areas around this number and if it's an IndexError
i.e. it's at the end of the list it ignores that case
"""
start = in_list(1, tour)
i = 2
counter = len(tour) * len(tour)
while True:
counter -= 1
print(i)
print(start)
if(start[1] > 0 and tour[start[0]][start[1] - 1] == i): #starting from checking the number on the left and going counter-clockwise
i += 1
start[1] -= 1
elif(start[0] < len(tour)-1 and start[1] > 0 and tour[start[0] + 1][start[1] - 1] == i):
i += 1
start[0] += 1
start[1] -= 1
elif(start[0] < len(tour)-1 and tour[start[0] + 1][start[1]] == i):
i += 1
start[0] += 1
elif(start[0] < len(tour)-1 and start[1] < len(tour)-1 and tour[start[0] + 1][start[1] + 1] == i):
i += 1
start[0] += 1
start[1] += 1
elif(start[1] < len(tour)-1 and tour[start[0]][start[1] + 1] == i):
i += 1
start[1] += 1
elif(start[0] > 0 and start[1] < len(tour)-1 and tour[start[0] - 1][start[1] + 1] == i):
i += 1
start[0] -= 1
start[1] += 1
elif(start[0] > 0 and tour[start[0] - 1][start[1]] == i):
i += 1
start[0] -= 1
elif(start[0] > 0 and start[1] > 0 and tour[start[0] - 1][start[1] - 1] == i):
i += 1
start[0] -= 1
start[1] -= 1
if(i == (len(tour) * len(tour))):
return True
break
elif(counter == 0):
return False
break
tour = [ [3, 2, 1,],
[4, 7, 9,],
[5, 6, 8,], ]
is_king_tour_3by3(tour)
所以我有一个二维列表,名为 tour:
tour = [ [3, 2, 1,],
[4, 7, 9,],
[5, 6, 8,], ]
我想让我的代码检查这个列表,看看国王(国际象棋)是否可以正常通过它。
我编写的代码可以适用于 N×N 二维列表。它所做的是从数字 1 开始,检查它周围的所有 9 个空格是否有数字 2,重复该过程直到 N×N。问题是,当数字位于列表的一侧时,代码会给出一个 IndexError,为了克服这个问题,我做了一个 try: except: IndexError
但现在当它到达 i = 6
时,它会继续无限地 [=14] =]
def in_list(number, listname):
"""
this function detects where a certain number is in a 2d list and returns the index of
it
"""
for i, sublist in enumerate(listname):
if (number in sublist):
return [i, sublist.index(number)]
return -1
def is_king_tour_3by3(tour):
"""
this function checks for all 9 areas around this number and if it's an IndexError
i.e. it's at the end of the list it ignores that case
"""
start = in_list(1, tour)
i = 2
counter = len(tour) * len(tour)
while True:
counter -= 1
print(i)
print(start)
try:
if(tour[start[0]][start[1] - 1] == i): #starting from checking the number on the left and going counter-clockwise
i += 1
start[1] -= 1
elif(tour[start[0] + 1][start[1] - 1] == i):
i += 1
start[0] += 1
start[1] -= 1
elif(tour[start[0] + 1][start[1]] == i):
i += 1
start[0] += 1
elif(tour[start[0] + 1][start[1] + 1] == i):
i += 1
start[0] += 1
start[1] += 1
elif(tour[start[0]][start[1] + 1] == i):
i += 1
start[1] += 1
elif(tour[start[0] - 1][start[1] + 1] == i):
i += 1
start[0] -= 1
start[1] += 1
elif(tour[start[0] - 1][start[1]] == i):
i += 1
start[0] -= 1
elif(tour[start[0] - 1][start[1] - 1] == i):
i += 1
start[0] -= 1
start[1] -= 1
except IndexError:
pass
if(i == (len(tour) * len(tour))):
return True
break
elif(counter == 0):
return False
break
通过将 start[0] < len(tour)-1 and start[1] > 0
添加到行中,您可以检查您是否不在二维列表之外,而不是使用 try: except:
。只要 start is > 0 and < len(tour)
(if start[0] or start[1] is -1 or len(tour)
it might fail),此代码将解决它:
def in_list(number, listname):
"""
this function detects where a certain number is in a 2d list and returns the index of
it
"""
for i, sublist in enumerate(listname):
if (number in sublist):
return [i, sublist.index(number)]
return -1
def is_king_tour_3by3(tour):
"""
this function checks for all 9 areas around this number and if it's an IndexError
i.e. it's at the end of the list it ignores that case
"""
start = in_list(1, tour)
i = 2
counter = len(tour) * len(tour)
while True:
counter -= 1
print(i)
print(start)
if(start[1] > 0 and tour[start[0]][start[1] - 1] == i): #starting from checking the number on the left and going counter-clockwise
i += 1
start[1] -= 1
elif(start[0] < len(tour)-1 and start[1] > 0 and tour[start[0] + 1][start[1] - 1] == i):
i += 1
start[0] += 1
start[1] -= 1
elif(start[0] < len(tour)-1 and tour[start[0] + 1][start[1]] == i):
i += 1
start[0] += 1
elif(start[0] < len(tour)-1 and start[1] < len(tour)-1 and tour[start[0] + 1][start[1] + 1] == i):
i += 1
start[0] += 1
start[1] += 1
elif(start[1] < len(tour)-1 and tour[start[0]][start[1] + 1] == i):
i += 1
start[1] += 1
elif(start[0] > 0 and start[1] < len(tour)-1 and tour[start[0] - 1][start[1] + 1] == i):
i += 1
start[0] -= 1
start[1] += 1
elif(start[0] > 0 and tour[start[0] - 1][start[1]] == i):
i += 1
start[0] -= 1
elif(start[0] > 0 and start[1] > 0 and tour[start[0] - 1][start[1] - 1] == i):
i += 1
start[0] -= 1
start[1] -= 1
if(i == (len(tour) * len(tour))):
return True
break
elif(counter == 0):
return False
break
tour = [ [3, 2, 1,],
[4, 7, 9,],
[5, 6, 8,], ]
is_king_tour_3by3(tour)