print "This is a program to help give you an idea which high-end PDA you should consider buying." print "While there are any number of companies from which you might buy a PDA, this program considers only the 8 market leadersper IDC. " print "The program will ask you to input a ranking or weighting for a number of criteria" print "that may be of importance in to you in choosing your next PDA. When prompted, please type in a number from 1 to 100 for each criteria, with" print "100 being of utmost importance to you, and 1 being of almost no importance to you." 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 options = ["Sharp", "Compaq", "HP", "Sony", "Palm", "Handspring", "Casio", "Research in Motion"] criteria = ["computing power", "expandability", "size of existing user base", "ease of cell phone integration", "breadth of tools for developers", "cool factor", "cost or price?", "bluetooth and/or wireless capabilities"] 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 = {("Sharp", "computing power"):100, ("Sharp", "expandability"):100, ("Sharp", "size of existing user base"):20, ("Sharp", "ease of cell phone integration"):20, ("Sharp", "breadth of tools for developers"):100, ("Sharp", "cool factor"):100, ("Sharp", "cost or price?"):80, ("Sharp", "bluetooth and/or wireless capabilities"):50, ("Compaq", "computing power"):100, ("Compaq", "expandability"):50, ("Compaq", "size of existing user base"):50, ("Compaq", "ease of cell phone integration"):20, ("Compaq", "breadth of tools for developers"):50, ("Compaq", "cool factor"):50, ("Compaq", "cost or price?"):100, ("Compaq", "bluetooth and/or wireless capabilities"):100, ("HP", "computing power"):100, ("HP", "expandability"):80, ("HP", "size of existing user base"):30, ("HP", "ease of cell phone integration"):20, ("HP", "breadth of tools for developers"):50, ("HP", "cool factor"):50, ("HP", "cost or price?"):100, ("HP", "bluetooth and/or wireless capabilities"):100, ("Sony", "computing power"):80, ("Sony", "expandability"):80, ("Sony", "size of existing user base"):30, ("Sony", "ease of cell phone integration"):50, ("Sony", "breadth of tools for developers"):50, ("Sony", "cool factor"):30, ("Sony", "cost or price?"):80, ("Sony", "bluetooth and/or wireless capabilities"):100, ("Palm", "computing power"):20, ("Palm", "expandability"):75, ("Palm", "size of existing user base"):100, ("Palm", "ease of cell phone integration"):10, ("Palm", "breadth of tools for developers"):30, ("Palm", "cool factor"):10, ("Palm", "cost or price?"):10, ("Palm", "bluetooth and/or wireless capabilities"):100, ("Handspring", "computing power"):50, ("Handspring", "expandability"):100, ("Handspring", "size of existing user base"):70, ("Handspring", "ease of cell phone integration"):100, ("Handspring", "breadth of tools for developers"):10, ("Handspring", "cool factor"):50, ("Handspring", "cost or price?"):10, ("Handspring", "bluetooth and/or wireless capabilities"):100, ("Casio", "computing power"):15, ("Casio", "expandability"):50, ("Casio", "size of existing user base"):50, ("Casio", "ease of cell phone integration"):10, ("Casio", "breadth of tools for developers"):1, ("Casio", "cool factor"):1, ("Casio", "cost or price?"):10, ("Casio", "bluetooth and/or wireless capabilities"):1, ("Research in Motion", "computing power"):10, ("Research in Motion", "expandability"):10, ("Research in Motion", "size of existing user base"):10, ("Research in Motion", "ease of cell phone integration"):100, ("Research in Motion", "breadth of tools for developers"):1, ("Research in Motion", "cool factor"):100, ("Research in Motion", "cost or price?"):10, ("Research in Motion", "bluetooth and/or wireless capabilities"):100,} # Calculate the resulting score for each option. # 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 "Results, in order from highest to lowest score" 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) award = result winner = option for option,result in results: if result > award: award = result winner = option print "Go ahead and buy a %s !!!" % winner