说我有以下代码:
command = input("prompt :> ").strip().lower()
try:
command = command.split()
# My input will be something like AB 1 2
x = int(command[1])-1
y = int(command[2])-1
if command[0] == "ab":
A_to_B(heights[x],distances[y])
高度是列表列表,距离也是如此:
heights = [['1','2'],['3','4'],['5','6']]
distances = [['a','b'],['c','d'],['e','f']]
所以我输入AC 1 2
x = 0
y = 1
然后我将从高处的0索引处获取列表,并从距离的1索引处列出列表。但是我继续遇到IndexError:列表超出范围。我怎样才能解决这个问题?提前谢谢!
为了简单起见,我在上面的代码中改变了很多名字。
这是我实际的A_to_B功能的样子
def tableau_to_foundation(tab, fnd):
'''Determines if movements from the tableaus to the foundations are valid.'''
if len(tab) == 0:
raise RuntimeError("Your tableau is empty.")
src_card = tab[-1]
dest_card = None
valid_fnd_move(src_card, dest_card)
fnd.append(tab.pop())
这里有valid_fnd_move(src_card,dest_card)
def valid_fnd_move(src_card, dest_card):
'''Determines if the initial moves done to the foundation are valid. '''
#First Check for when foundation is empty
if dest_card == None and src_card.rank() == 1:
return
#Second Check for when foundation is empty
if dest_card == None and src_card.rank() != 1:
raise RuntimeError("You need to place an ace into the foundation first.")
#Checks if foundation is not empty
if not dest_card.suit() == src_card.suit() and \
src_card.rank == dest_card.rank() + 1:
raise RuntimeError("You can only place cards of the same suit together in a particular foundation")