General Decision Menu (Basketball, Programming Lnag. gerneral etcetc
Ron Stephens
rdsteph at earthlink.net
Fri Apr 13 23:31:05 EDT 2001
I guess i better stop posting these long spaghetti code things, but this
is the first pass at a menu based general decision analysis thing...it
has four parts, one is general any question, two is programming language
choosier expert system , tow is scripting language chooser, four is a
basketball predictor expert sysstem...does anyone know of any Python
expert system type of programs I could look at are??? I am interested in
any artificial intelligence Python programs or Expert System Python
programs...
anyway...this program runs fine under Python Win but fails under IDLE
for unknown reasons..."Tcl error expected boolean got??" after I give it
the first answer to the first prompt...
Decision_Analysis_Beta3.py
print "A program to help you make decisions."
print
print
def decisionanalysis():
# This code is placed in the public domain
def get_list(heading, prompt):
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):
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 = get_list("Enter your options", "Option %d: ")
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.
# 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 "Scores for option %s" % 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:
print o, c, rankings[c], score[o, c]
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)
def ProgramLanguageFinal():
print "This is a program to help give you an idea which programming
languages you should consider learning. While there are any number of
languages you might consider, this program considrs only 11 of teh most
popluar ones. The program will ask you to input a ranking or weighting
for a number of criteria that may be of importance in choosing your next
programming language."
def get_list(heading, prompt):
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 = ["Python", "Perl", "Ruby", "Tcl", "JavaScript", "Visual
Basic", "Java", "C++", "C", "Lisp", "Delphi"]
criteria = ["ease of learning", "ease of use", "speed of program
execution", "quality of available tools", "popularity", "power &
expressiveness", "cross platform?", "cost"]
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 = {("Python", "ease of learning"):100, ("Python", "ease of
use"):100, ("Python", "speed of program execution"):10, ("Python",
"quality of available tools"):70, ("Python", "popularity"):50,
("Python", "power & expressiveness"):100, ("Python", "cross
platform?"):100, ("Python", "cost"):100,
("Perl", "ease of learning"):50, ("Perl", "ease of use"):90,
("Perl", "speed of program execution"):30, ("Perl", "quality of
available tools"):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
available tools"):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 available
tools"):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", "quality of available tools"):50, ("JavaScript",
"popularity"):100, ("JavaScript", "power & expressiveness"):40,
("JavaScript", "cross platform?"):50, ("JavaScript", "cost"):100,
("Visual Basic", "ease of learning"):50, ("Visual Basic", "ease of
use"):100, ("Visual Basic", "speed of program execution"):20, ("Visual
Basic", "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
available tools"):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 available
tools"):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
of program execution"):100, ("C", "quality of available tools"):100,
("C", "popularity"):100, ("C", "power & expressiveness"):100, ("C",
"cross platform?"):110, ("C", "cost"):100,
("Lisp", "ease of learning"):20, ("Lisp", "ease of use"):30,
("Lisp", "speed of program execution"):70, ("Lisp", "quality of
available tools"):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 of
available 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
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)
def ProgramLanguageScript():
def get_list(heading, prompt):
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):
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 = ["Python", "Perl", "Ruby", "Tcl", "JavaScript", "Visual
Basic"]
criteria = ["ease of learning", "ease of use", "speed of program
execution", "quality of available tools", "popularity", "power &
expressiveness", "cross platform?", "cost"]
# 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 = {("Python", "ease of learning"):100, ("Python", "ease of
use"):100, ("Python", "speed of program execution"):10, ("Python",
"quality of available tools"):50, ("Python", "popularity"):50,
("Python", "power & expressiveness"):100, ("Python", "cross
platform?"):100, ("Python", "cost"):100,
("Perl", "ease of learning"):50, ("Perl", "ease of use"):90,
("Perl", "speed of program execution"):30, ("Perl", "quality of
available tools"):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"):10, ("Ruby", "quality of
available tools"):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"):5, ("Tcl", "quality of available
tools"):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", "quality of available tools"):50, ("JavaScript",
"popularity"):100, ("JavaScript", "power & expressiveness"):40,
("JavaScript", "cross platform?"):50, ("JavaScript", "cost"):100,
("Visual Basic", "ease of learning"):50, ("Visual Basic", "ease of
use"):100, ("Visual Basic", "speed of program execution"):20, ("Visual
Basic", "quality of available tools"):100, ("Visual Basic",
"popularity"):100, ("Visual Basic", "power & expressiveness"):50,
("Visual Basic", "cross platform?"):1, ("Visual Basic", "cost"):1}
# 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:
print o, c, rankings[c], score[o, c]
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)
def Basketball():
print "This is a program to help you decide which team will win a
basketball game"
print
print "When prompted, enter a number ranking each team on the
prompted team skill on a scle form 1 to 100, with 1 being terrible and
100 being the best imaginable"
print
team_one = raw_input ("What is the name of team one:")
team_two = raw_input ("What is the name of team two:")
criteria = {"speed":100, "size":66, "jumping_ability":50,
"defense":60, "shooting":75, "ballhandling":50, "rebounding":50}
scoreonespeed = input ("rank the team speed of %s on a scale of 1 to
100:" % team_one)
scoretwospeed = input ("rank the team speed of %s on a scale of 1 to
100:" % team_two)
scoreonesize= input ("rank the team size of %s on scale of 1 to 100"
% team_one)
scoretwosize= input ("rank the team size of %s on scale of 1 to 100"
% team_two)
scoreonejumping_ability= input("rank the jumping ability of %s" %
team_one)
scoretwojumping_ability= input("rank the jumping ability of %s" %
team_two)
scoreonedefense = input ("ramk the defense of %s" % team_one)
scoretwodefense = input ("ramk the defense of %s" % team_two)
scoreoneshooting = input ("rank the shooting ability of %s" %
team_one)
scoretwoshooting = input ("rank the shooting ability of %s" %
team_two)
scoreoneballhandling= input("rank the ballhandling ability of %s:" %
team_one)
scoretwoballhandling= input("rank the ballhandling ability of %s:" %
team_two)
scoreonerebounding = input ("rank the rebounding ability of %s" %
team_one)
scoretworebounding = input ("rank the rebounding ability of %s" %
team_two)
scoreteamone = (criteria["speed"])*(scoreonespeed) +
(criteria["size"])*(scoreonesize)
+(criteria["jumping_ability"])*(scoreonejumping_ability)
+(criteria["defense"])*(scoreonedefense) +
(criteria["shooting"])*(scoreoneshooting)
+(criteria["ballhandling"])*(scoreoneballhandling)
+(criteria["rebounding"])*(scoreonerebounding)
scoreteamtwo = (criteria["speed"])*(scoretwospeed) +
(criteria["size"])*(scoretwosize)
+(criteria["jumping_ability"])*(scoretwojumping_ability)
+(criteria["defense"])*(scoretwodefense) +
(criteria["shooting"])*(scoretwoshooting)
+(criteria["ballhandling"])*(scoretwoballhandling)
+(criteria["rebounding"])*(scoretworebounding)
print "%s has a power ranking of %d" % (team_one, scoreteamone)
print
print "%s has a power ranking of %d" % (team_two, scoreteamtwo)
if scoreteamone > scoreteamtwo:
print "%s wins!!!" % team_one
elif scoreteamone == scoreteamtwo:
print "the two teams are a toss-up!!!"
else:
print "%s wins!!!" % team_two
print "Please enter the number for the type of decision you wish to
analayze:"
print "1. General Decision Analysis, you choose the options, criteria,
etc."
print "2. Help in Choosing Programming Language amongst 11 popular
languages"
print "3. Help in choosing scripting programming language amongst 6
scripting languages"
print "4. Which Basketball Team will win the Game???"
while 1: # loop forever
choice = input("Please type in the number of the type of
decision-program you wish to run from above and hit enter:")
if choice ==1:
decisionanalysis()
elif choice ==2:
ProgramLanguageFinal()
elif choice ==3:
ProgramLanguageScript()
elif choice ==4:
Basketball()
elif choice =="quit":
break # exit from infinite loop
else:
print "Invalid operation"
More information about the Python-list
mailing list