A
A
Azamgl2021-11-21 13:42:35
Python
Azamgl, 2021-11-21 13:42:35

How to assign an index to a single element?

import random
ch1 = {'name': 'x, 'damage':10, 'hp':130, 'miss':0.1, 'dodge':0.7}
ch2 = {'name': 'y', 'damage':40, 'hp':160, 'miss':0.5, 'dodge':0.4}
ch3 = {'name': 'z', 'damage':70, 'hp':180, 'miss':0.7, 'dodge':0.2}
ch4 = {'name': 'f', 'damage':30, 'hp':140, 'miss':0.3, 'dodge':0.6}
a = random.randint(1,4)
b = random.randint(1,4)
while a==b:
    b=random.randint(1,4)

Let's say through random it turned out that a \u003d 3. How can I output a third ch3 dictionary named z? Those. something like print(cha), where 3 would be substituted instead of a and then all values ​​would be taken from the 3rd dictionary.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2021-11-21
@Azamgl

items = [ch1, ch2, ch3, ch4]
ch = items[a]

Just pay attention, indexes start from zero.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question