import random #Getting the random library, if you download Python this library should already exist.class ai: #The AI class def __init__(self, actions, responses): '''Never use this function. To call it, just do:var = ai(action list, response list)''' self.IN = actions self.OUT = responses def get_act(self, action, valres): '''Gets the AI action from the user action. valres is the response number (If the first response fails, try the next one. Handle this number in your code.)''' if action in self.IN: #Does the AI know the action? mList = {} for response in self.OUT: #gets all the valid responses if self.IN[self.OUT.index(response)] == action and not response in mList: mList[response] = 1 elif response in mList: mList[response] += 1 print mList keys = [] vals = [] for v in sorted(mList.values(), reverse = True): '''puts the responses into the correct order''' for k in mList.keys(): if mList[k] == v: keys.append(k) vals.append(v) print keys print vals try: resp = keys[valres] #does a listed response except: resp = random.choice(self.OUT) #if an indexError occurs, do this else: #AI chooses a random response. resp = random.choice(self.OUT) return resp #gives back the response, so set a variable to get_act().import random Getting the random library, if you download Python this library should already exist.
class ai:The AI class
def __init__(self, actions, responses): self.IN = actions self.OUT = responsesNever use this function. To call it, just do:
var = ai(action list, response list)def get_act(self, action, valres): if action in self.IN: mList = {} for response in self.OUT: if self.IN[self.OUT.index(response)] == action and not response in mList: mList[response] = 1 elif response in mList: mList[response] += 1 print mList keys = [] vals = [] for v in sorted(mList.values(), reverse = True): for k in mList.keys(): if mList[k] == v: keys.append(k) vals.append(v) print keys print vals try: resp = keys[valres] except: resp = random.choice(self.OUT) else: resp = random.choice(self.OUT) return respGets the AI action from the user action. valres is the response number (If the first response fails, try the next one. Handle this number in your code.)
if action in self.IN: mList = {} for response in self.OUT: if self.IN[self.OUT.index(response)] == action and not response in mList: mList[response] = 1 elif response in mList: mList[response] += 1 print mList keys = [] vals = [] for v in sorted(mList.values(), reverse = True): for k in mList.keys(): if mList[k] == v: keys.append(k) vals.append(v) print keys print vals try: resp = keys[valres] except: resp = random.choice(self.OUT)Does the AI know the action?
for response in self.OUT: if self.IN[self.OUT.index(response)] == action and not response in mList: mList[response] = 1 elif response in mList: mList[response] += 1Gets all the valid responses
for v in sorted(mList.values(), reverse = True): for k in mList.keys(): if mList[k] == v: keys.append(k) vals.append(v)puts the responses into the correct order
try: resp = keys[valres]Does a listed response
except: resp = random.choice(self.OUT) If an indexError occurs, do this
else: resp = random.choice(self.OUT)AI chooses a random response.
return resp #gives back the response, so set a variable to get_act().