def get_list(heading, prompt): """ get_list(heading, prompt) -> list This function prompts for a list of things. The heading is printed on a line by itself, and the prompt must have a %d substitution for the number of the item within the list. """ print heading print print "(enter a blank line to end the list)" ret = [] i = 1 while 1: line = raw_input(prompt % i) if not line: break ret.append(line) i=i+1 print return ret def get_number(prompt): """ get_number(prompt) -> float This function prompts for a number. If the user enters bad input, such as "cat" or "3l", it will prompt again. """ res = None while res is None: try: res = float(raw_input(prompt)) except ValueError: pass return res # First, ask the user to enter the lists options = ["Yes", "No"] criteria = get_list("Enter your criteria ...", "Criterion %d: ") # Next, get the user to rank his criteria. I use a system where higher # is better, so that an undesirable characteristic can be given a negative # weight. # # {} is a dictionary, it can be indexed by (nearly) any expression, # and we will index it with the names of the criteria. # (For a more traditional program, we could use a list and index by the # number of the criterion) rankings = {} print print "Enter relative importance of criteria (higher is more important)" print for c in criteria: rankings[c] = get_number("Criterion %s: " % c) # Next, get the user to score each option on all the criteria. # Here, we index the dictionary on the pair (option, criterion). # This is similar to a two-dimensional array in other languages score = {} print print "Enter score for each option on each criterion" print for o in options: print print "Score for a %s decision "% o print for c in criteria: score[o, c] = get_number("Criterion %s: " % c) # Calculate the resulting score for each option. This equation # is different from Rod Stephen's original program, because I # make more important criteria have higher rankings, and even let # bad criteria have negative rankings. # The "result" dictionary is indexed with the names of the options. result = {} for o in options: value = 0 for c in criteria: value = value + rankings[c] * score[o, c] result[o] = value # Now, I want to take the dictionary result, and turn it into a ranked list results = result.items() # A list of tuples (key, value) results.sort(lambda x, y: -cmp(x[1], y[1])) # Sort the list using the reverse of the # "value" of the entry, so that higher # values come first print print "The results are" print print "%5s %s" % ("Score", "Option") # Take the pairs out of results in order, and print them out for option, result in results: print "%5s %s" % (result, option) if results[0] > results[1]: print "You should decide Yes!!!" else: print "You should decide No!!!"