2013-06-27 182 views
1

我試圖在python中編寫決策矩陣,到目前爲止,我已經設置了2維矩陣,設置了列表來保存決策選項和標準的標題,獲取金額(選項和標準)以及來自用戶的標題,然後從用戶那裏獲取標準和選項的值。IndexError:我做錯了什麼?

我想移動到下一個步驟,使得基於這樣的價值觀的決定,但我得到這個錯誤:

Traceback (most recent call last): File "/home/michael/std/dev/python/DM/DM0.0.2.py", line 56, in <module> print "\nOut of 10, how do you rate option %s for criteria %s?" % (optionNames[optionCount], criteriaNames[criteriaCount]) IndexError: list index out of range

我已經試過各種修復它,重新安排代碼使用流量控制等幾種風格,我無法弄清楚。正如你從下面的完整代碼中可以看到的那樣,我已經包含了一些打印語句,讓我自己對代碼的確切情況有一些反饋,這對我來說似乎是正確的。有人能告訴我我要去哪裏錯了,請?謝謝。

# DM2 - Decision Matrix 2 

print "Decision Matrix" 
print "Please enter integers" 
bl = "------------------------------" 
print bl 
# Get the ammount of options and criteria to work with 
print "How many options do you have?" 
num_options = int(raw_input("> ")) 
print bl 

print "How many criteria do you have?" 
num_criteria = int(raw_input("> ")) 
print bl 

# Define some lists to keep names of options and criteria 
optionNames = [0 for x in range(0, num_options)] 
criteriaNames = [0 for x in range(0, num_criteria)] 

# Define our matrix 
Matrix = [[0 for x in range(0, num_criteria)] for x in range(0, num_options)] 

# Get names for options and criteria 
count = 0 
while count < num_options: 
    modCount = count +1 
    optionNames[count] = raw_input("Name option %s:" % modCount) 
    count = count +1 

print optionNames 
print bl 

count = 0 
while count < num_criteria: 
    modCount = count +1 
    criteriaNames[count] = raw_input("Name criteria %s:" % modCount) 
    count = count +1 

print criteriaNames 
print bl 

# Print the matrix so far (should be empty) 
print "The Matrix:" 
print Matrix 
print bl 

print "\nNow we will talk about how well the options match the criteria." 

#take scores out of 10 for the matrix 
optionCount = 0 
criteriaCount = 0 

while criteriaCount < num_criteria * num_options +1: 
    while optionCount < num_options: 
     print "\nOut of 10, how do you rate option %s for criteria %s?" % (optionNames[optionCount], criteriaNames[criteriaCount]) 
     print Matrix 
     print "opCount: ", optionCount 
     print "critCount: ", criteriaCount 
     criteriaScore = int(raw_input("> ")) 
     Matrix[optionCount][criteriaCount] = criteriaScore 
     optionCount +=1 
    criteriaCount +=1 
    optionCount = 0 
    while criteriaNames < num_criteria * num_options +1: 
     while criteriaCount < num_criteria: 
      print "\nOut of 10, how do you rate option %s for criteria %s?" % (optionNames[optionCount], criteriaNames[criteriaCount]) 
      print Matrix 
      print "opCount: ", optionCount 
      print "critCount: ", criteriaCount 
      criteriaScore = int(raw_input("> ")) 
      Matrix[optionCount][criteriaCount] = criteriaScore 
      optionCount +=1 



# Calculate best decision based on the best scoring option 
+0

已排序。 使用double for循環是解決方案,非常感謝。 雖然循環太多,但我也嘗試添加建議的打印和criteriaCount並沒有比len(criteriaNames)更高。 – 66Tricks

回答

0

據我所知,你想輸入每個條件和選項組合的分數。

問題在於你的三段時間搞砸了索引。一個更簡潔的方法將是循環雙:

for option in xrange(num_options): 
    for criteria in xrange(num_criteria): 
     # read input 
     Matrix[option][criteria] = score 

更清潔列表初始化,您可以使用

[0]*num_options 

如果我的理解是正確的,它與您的期望,現在試圖重寫這段代碼到,而循環理解錯誤。

0

如果打印optionCountcriteriaCountlen(optionNames)len(criteriaNames),那叫聲打印語句之前,你會看到,你是最有可能處理不當你的索引,或者你的*Names列表不填充爲你」 d期望。

看看代碼,我敢打賭criteriaCountlen(criteriaNames)高。只是一點賭博雖然:)