Movatterモバイル変換
[0]ホーム
Program to Choose a Programming Language: Recursive Decision Analysis 101...
Ron Stephensrdsteph at earthlink.net
Fri Apr 13 23:24:06 EDT 2001
With the help of good folks on this list, the following Python programhas been created to help a newbie choose between and amongst 11languages, namely Java, Visual Basic, C++, C, Lisp, Delphi (Pascal),JavaScript, Python, Perl, Ruby, and Tcl...Now, I keep coming up with Python, given my preferences, but yourmileage may vary...Problems I still have1. This program works fine for me when I run it under pythonWin, butwhen I try to run it under IDLE, I get an error message after firstprompt, "Tcl error, expected boolean got ???' Why would a program runfine under pythonwin but not under IDLE (on Win(98))2. It's hard to make a program look nice, input and output wise, whenthe inputs and outputs are so different under pythonwin vs IDLE, vs atthe Python DOS shell prompt...makes me want to run under Tkinter but Istill have not been able to figure that out..but there should be anacceptable way to run under command line under all IDE's??Here is the program called ProgramLanguageFinal.py:::(its still bit ofspaghetti code even though I used good code form this list' members butI probably butchered it...(with huge apologies to Jeff Epler)...print "This is a program to help give you an idea which programminglanguages you should consider learning."print "While there are any number of languages you might consider, thisprogram considers only 11 of the most"print "popular ones. The program will ask you to input a ranking orweighting for a number of criteria"print "that may be of importance in choosing your next programminglanguage."def get_list(heading, prompt): """ get_list(heading, prompt) -> listThis function prompts for a list of things. The heading is printed ona line by itself, and the prompt must have a %d substitution for thenumber 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 retdef get_number(prompt): """ get_number(prompt) -> floatThis function prompts for a number. If the user enters bad input, suchas"cat" or "3l", it will prompt again.""" res = None while res is None: try: res = float(raw_input(prompt)) except ValueError: pass return resoptions = ["Python", "Perl", "Ruby", "Tcl", "JavaScript", "VisualBasic", "Java", "C++", "C", "Lisp", "Delphi"]criteria = ["ease of learning", "ease of use", "speed of programexecution", "quality of available tools", "popularity", "power &expressiveness", "cross platform?", "cost"]rankings = {}printprint "Enter relative importance of criteria (higher is more important)"printfor 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 languagesscore = {("Python", "ease of learning"):100, ("Python", "ease ofuse"):100, ("Python", "speed of program execution"):10, ("Python","quality of available tools"):70, ("Python", "popularity"):50,("Python", "power & expressiveness"):100, ("Python", "crossplatform?"):100, ("Python", "cost"):100,("Perl", "ease of learning"):50, ("Perl", "ease of use"):90, ("Perl","speed of program execution"):30, ("Perl", "quality of availabletools"):50, ("Perl", "popularity"):75, ("Perl", "power &expressiveness"):100, ("Perl", "cross platform?"):100, ("Perl","cost"):100,("Ruby", "ease of learning"):50, ("Ruby", "ease of use"):100, ("Ruby","speed of program execution"):20, ("Ruby", "quality of availabletools"):20, ("Ruby", "popularity"):10, ("Ruby", "power &expressiveness"):100, ("Ruby", "cross platform?"):80, ("Ruby","cost"):100,("Tcl", "ease of learning"):100, ("Tcl", "ease of use"):100, ("Tcl","speed of program execution"):10, ("Tcl", "quality of availabletools"):50, ("Tcl", "popularity"):40, ("Tcl", "power &expressiveness"):10, ("Tcl", "cross platform?"):100, ("Tcl","cost"):100,("JavaScript", "ease of learning"):70, ("JavaScript", "ease of use"):75,("JavaScript", "speed of program execution"):10, ("JavaScript", "qualityof available tools"):50, ("JavaScript", "popularity"):100,("JavaScript", "power & expressiveness"):40, ("JavaScript", "crossplatform?"):50, ("JavaScript", "cost"):100,("Visual Basic", "ease of learning"):50, ("Visual Basic", "ease ofuse"):100, ("Visual Basic", "speed of program execution"):20, ("VisualBasic", "quality of available tools"):100, ("Visual Basic","popularity"):100, ("Visual Basic", "power & expressiveness"):50,("Visual Basic", "cross platform?"):1, ("Visual Basic", "cost"):1,("Java", "ease of learning"):15, ("Java", "ease of use"):50, ("Java","speed of program execution"):50, ("Java", "quality of availabletools"):100, ("Java", "popularity"):100, ("Java", "power &expressiveness"):100, ("Java", "cross platform?"):100, ("Java","cost"):100,("C++", "ease of learning"):10, ("C++", "ease of use"):25, ("C++","speed of program execution"):90, ("C++", "quality of availabletools"):100, ("C++", "popularity"):100, ("C++", "power &expressiveness"):100, ("C++", "cross platform?"):100, ("C++","cost"):100,("C", "ease of learning"):15, ("C", "ease of use"):10, ("C", "speed ofprogram execution"):100, ("C", "quality of available tools"):100, ("C","popularity"):100, ("C", "power & expressiveness"):100, ("C", "crossplatform?"):110, ("C", "cost"):100,("Lisp", "ease of learning"):20, ("Lisp", "ease of use"):30, ("Lisp","speed of program execution"):70, ("Lisp", "quality of availabletools"):50, ("Lisp", "popularity"):25, ("Lisp", "power &expressiveness"):110, ("Lisp", "cross platform?"):80, ("Lisp","cost"):90,("Delphi", "ease of learning"):50, ("Delphi", "ease of use"):110,("Delphi", "speed of program execution"):85, ("Delphi", "quality ofavailable tools"):100, ("Delphi", "popularity"):30, ("Delphi", "power &expressiveness"):100, ("Delphi", "cross platform?"):80, ("Delphi","cost"):10}# 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 rankedlistresults = 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 firstprintprint "Results, in order from highest to lowest score"printprint "%5s %s" % ("Score", "Option")# Take the pairs out of results in order, and print them outfor option, result in results: print "%5s %s" % (result, option)
More information about the Python-listmailing list
[8]ページ先頭