我正在尝试在python中编写决策矩阵,到目前为止我已经设置了2维矩阵,设置列表以保存决策选项和标准的标题,获取金额(选项和标准)来自用户的标题和标题,然后从用户获取标准和选项的值。
我想继续下一步,根据这些值做出决定,但我收到了这个错误:
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 :(得分:0)
如果您在吠叫的打印语句之前打印optionCount
,criteriaCount
,len(optionNames)
,len(criteriaNames)
,您会发现您很可能处理错误的索引,或者您的*Names
列表未按预期填充。
查看代码,我敢打赌criteriaCount
高于len(criteriaNames)
。只是有点赌博:)
答案 1 :(得分:0)
据我了解,您希望为每个条件和选项组合输入分数。
问题在于你的三个问题搞砸了索引。一个更简洁的方法将是一个双循环:
for option in xrange(num_options):
for criteria in xrange(num_criteria):
# read input
Matrix[option][criteria] = score
对于清洁列表初始化,您可以使用
[0]*num_options
如果我的理解是正确的并且符合您的期望,现在尝试将此代码重写为while循环以理解错误。