Python Forum
How do I add an element to a dic and increase an existing value - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How do I add an element to a dic and increase an existing value (/thread-22656.html)



How do I add an element to a dic and increase an existing value - Kanashi - Nov-21-2019

Hello

I'm extremely new to coding, I started learning Python two days ago. And I'm working on a simple text based battle game, while following a tutorial.

I want to add a "weapon" or "item" to the player dic:
player = {'name': 'Timmy', 'health': 100, 'attack': 35, 'heal': 16}
This "weapon" is supposed to increase the "player['attack']" by lets say "10" attack points, therefor making the new attack value 35+10=45.
I'm also thinking of giving the enemy you are fighting a disarm ability, which will remove the "weapon" from the player and therefor return the attack value to it's original, "35".

This is the code for how the enemy's health is lowered once I attack him:
            monster['health'] = monster['health'] - player['attack']
My question is, what would be the most efficent way to code the "weapon" which will increase the "player['attack']" by 10, and have another code remove the "weapon" element from the player dic.

Thanks :)


RE: How do I add an element to a dic and increase an existing value - paul18fr - Nov-21-2019

Hi

I started to use dictionaries quite soon, and I did some tests; I think they may help you

Paul

import numpy as np
from sys import getsizeof

n = 10
m = 2
X = np.random.random((n,m))
Y = np.random.random((n,m))
Z = np.random.random((n,m))
MAT_DICT = {'abscissa': X, 'ordinate': Y, 'applicate': Z}
print("X RAM = {}".format(getsizeof(X)))
print("Y RAM = {}".format(getsizeof(Y)))
print("Z RAM = {}".format(getsizeof(Z)))
print("MAT_DICT RAM = {}".format(getsizeof(MAT_DICT)))

my_var = 'abscissa'
if my_var in MAT_DICT:
    print("'{}' is in MAT matrix".format(my_var))
else:
    print("'{}' is not in MAT matrix".format(my_var))

    
## add new key/value
theta = np.random.random((n,2*m))
MAT_DICT.update({'Theta': theta})

## access to a key and work with
extract_key = MAT_DICT["abscissa"]
check = np.min(np.absolute(X - extract_key))
print("diff = {}".format(check))


## now update of 'abcissa" values
extract_key = 2.*MAT_DICT["abscissa"]
MAT_DICT["abscissa"] = extract_key # solution 1

extract_key = 10.*MAT_DICT["ordinate"]
MAT_DICT.update({'ordinate': extract_key}) #solution2



RE: How do I add an element to a dic and increase an existing value - ThomasL - Nov-21-2019

Hi, have a look how it might be done.

# the initial start values
player = {'name': 'Timmy', 'health': 100, 'attack': 35, 'heal': 16, 'weapon': 0}
monster = {'health': 50}

# let´s introduce a weapons dict
weapons = {'barehands': 0, 'knife': 5, 'sword': 10}

# if player gets a sword
player['weapon'] += weapons['sword']
damage = player['attack'] + player['weapon']
monster['health'] -= damage
print(f'Damage done = {damage} Monster stats:{monster}')

# if monster has ability to disarm
monster = {'health': 50, 'disarm': True}
damage = player['attack'] + player['weapon'] - player['weapon'] * monster['disarm']
monster['health'] -= damage
print(f'Damage done = {damage} Monster stats:{monster}')

# if monster has lost ability to disarm
monster = {'health': 50, 'disarm': False}
damage = player['attack'] + player['weapon'] - player['weapon'] * monster['disarm']
monster['health'] -= damage
print(f'Damage done = {damage} Monster stats:{monster}')
Output:
Damage done = 45 Monster stats:{'health': 5} Damage done = 35 Monster stats:{'health': 15, 'disarm': True} Damage done = 45 Monster stats:{'health': 5, 'disarm': False}