所以我基本上我希望它循环三次,如你所见,我有选择 = 0,1,2 和m
范围 3 内的 for 所以这应该循环三次,首先是选择 0,然后是选择 1,然后是选择 2。那么为什么会超出范围呢?
Python的确切错误返回:
choices[r]=p.nextInt()
IndexError: list assignment index out of range
请忽略p.next
,因为这是我们必须在 uni 编写它的方式,因为我们有自己的模块。
这是我的代码
cities=["Coventry", "Birmingham", "Wolverhampton", "Leicester"]
distances=[
[0,25,33,24],
[25,0,17,42],
[33,17,0,54],
[24,42,54,0]]
def distancesthree():
choices=[0,1,2]
for m in range(3): #This will loop three times!
for r in range(len(cities)):
p.write ("%d : %s\n" %(r, cities[r]))
p.write("\nEnter city number %d: \n"%(m+1))
choices[r]=p.nextInt()