0
說我有這個下面的代碼:輸入當轉換爲整數超出範圍
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']]
所以我輸入交流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")
不知道這與你的IndexError有什麼關係,但你有一個第4行錯字'y = int命令[2]) - 1' –
我的索引錯誤仍然存在,但謝謝你的捕獲 – RGPython
這是Python 2還是Python 3? –