2015-11-01 119 views
0

不知道爲什麼我索引超出範圍錯誤。Python索引超出範圍錯誤

功能應該循環IP地址列表,直到事件值在:

event = ["Birthday-venues", "baby-shower", "wedding", "corp", "party"] 
ip = [1, 2, 3] 

lip = len(ip) 
levent = len(event) 


def eventIP(): 
    i = 0 
    dict = {} 
    while i < levent: 
     for j in range(0, lip): 
      dict[event[i]] = ip[j] 
      if i < levent: 
       i += 1 
      else: 
       break 

    print(dict) 

eventIP() 

錯誤:

Traceback (most recent call last): 
    File "script.py", line 21, in <module> 
    eventIP() 
    File "script.py", line 13, in eventIP 
    dict[event[i]] = ip[j] 
IndexError: list index out of range 
+0

您對此有何看法? – TigerhawkT3

+0

當它崩潰時,我和j的值是什麼? – AndreiM

+1

'levent = len(event)-1'呢?如果你這樣做,那麼輸出將是'{'生日場地':1,'派對':2,'嬰兒洗澡':2,'公司':1,'婚禮':3}'。 –

回答

2

它崩潰,因爲你考我是否更新前勒比小。請嘗試以下操作:

i += 1 
    if i >= levent: 
     break 
+0

完美作品,謝謝:) – reet