<jupyter_start><jupyter_text>Try to represent two characters with dictionaries, including functions as values<jupyter_code>aragorn={"Life":110, #Life points of the character
      "Strength": 10, # The power of the attack
      "Equip":{"sword":{"Strength":10,"Status":True}}, #equip is a dictionary of weapons, each weapon is a dict of characteristics
        #Attack function is the total strenght with which aragorn will attack
      "TotAttack":lambda x: x["Strength"]+x["Equip"]["sword"]["Strength"], #compute the total attack of the character. Strength+Equip
      "Attack":lambda x,y: x["Life"]-y['TotAttack'](y) } 

saruman={"Life":100,
      "Strength": 10,
      "Equip":{"sword":{"Strength":10,"Status":True}}, 
        #Attack function is the total strenght with which aragorn will attack
      "TotAttack":lambda x: x["Strength"]+x["Equip"]["sword"]["Strength"],
      "Attack":lambda x,y: x["Life"]-y['TotAttack'](y) }<jupyter_output><empty_output><jupyter_text>Better to define only characteristics in dictionaries and keep function defined outside<jupyter_code>aragorn={"Name": "Aragorn",
         "Life":110,
      "Strength": 10,
      "Equip":{"sword":{"Strength":10,"Status":True},
              "Bow":{"Strength":7,"Status":True}}}

saruman={"Name": "Saruman",
         "Life":100,
      "Strength": 10,
      "Equip":{"sword":{"Strength":10,"Status":False},
              "Stick":{"Strength":10,"Status":True}}}
def totAttack(character):
    '''
        The total attack of a character is given by its strength and the sum of the strength of its weapons.
        So the idea is to iterate with list comprehension over the weapons and get their strength if status s true
        and then add to this list the strength of the caracter. Finally sum this list.
    '''
    return sum([character['Strength']] + [x['Strength']*x['Status'] for x in character['Equip'].values()])
    
    
def attack(defense,attacker):
    '''
        This function subtract attacker TotalAttack to defender  
    '''
    # If the character is going to die, set its life to 0 and print 'Is dead'
    if defense['Life'] <=totAttack(attacker):
        print(defense["Name"] + ' is dead.')
        defense['Life']=0
        return 
    else: 
        defense['Life'] -= totAttack(attacker)
# if a just point to the same element a=saruman . Then if I modify saruman also a will change. Need 
#to put .copy() to create an other element in the memory!
a=saruman.copy()
a['Life']
print(saruman['Life'])
attack(defense = saruman,attacker = aragorn)
print(saruman['Life'])

print a['Life']
saruman['Life']<jupyter_output>73
{'fraction_non_alphanumeric': 0.14624808575803983, 'fraction_numerical': 0.01339969372128637, 'mean_word_length': 3.967680608365019, 'pattern_counts': {'":': 36, '<': 9, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '307428', 'license': 'MIT', 'n_tokens_mistral': 862, 'n_tokens_neox': 792, 'n_words': 248, 'path': '04/The Lord of the Classes.ipynb', 'repo_name': 'Py101/Lectures', 'size': '2612'}