Python Forum
How do I add an element to a dic and increase an existing value
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I add an element to a dic and increase an existing value
#1
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 :)
Reply
#2
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
Reply
#3
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}
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Increase the speed of a python loop over a pandas dataframe mcva 0 1,290 Jan-21-2022, 06:24 PM
Last Post: mcva
  How to increase the size of a png picture for the heatmap of the correlation? lulu43366 9 3,389 Oct-06-2021, 04:15 PM
Last Post: deanhystad
  Using SoX in Python to Increase mp3 Bitrate DRT 1 1,715 Jul-10-2021, 08:41 PM
Last Post: DRT
  Clicker count increase by 1 each time blakefindlay 1 5,530 Feb-03-2021, 03:50 PM
Last Post: deanhystad
  increase and decrease a slice value? KEYS 2 2,055 Nov-10-2020, 11:35 PM
Last Post: KEYS
  Increase Numbers forever and need reset? ATARI_LIVE 4 2,285 Oct-23-2020, 01:55 PM
Last Post: ATARI_LIVE
  Unable to locate element no such element gahhon 6 4,370 Feb-18-2019, 02:09 PM
Last Post: gahhon
  How to increase the data size SriRajesh 3 4,007 Nov-10-2018, 04:29 PM
Last Post: ichabod801
  Want to increase performance of adb call fabiofacir 0 2,846 Jun-30-2018, 11:39 AM
Last Post: fabiofacir
  Matplotlib Colorbar Ticks (Increase number) BennyS 1 8,779 Apr-18-2018, 04:00 PM
Last Post: BennyS

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020