0
>>> import random

#These are the operators guns

Rook = ['MP5','P90','SG-CQB']
Doc = ['MP5','P90','SG-CQB']
Mute = ['MP5K','M590A1']
Smoke = ['FMG-9','M590A1']

#string of all the operators below

Defending_operators = ['Smoke','Mute','Doc','Rook','Castle','Pulse','Kapkan','Tachanka','Jager','Bandit']

Defender = (random.choice(Defending_operators))

what i tried to do is print a random operator and then from what i got, choose a random gun in his load out but i dont know how to do this..

>>>print(Defender)
Rook

>>>if Defender == Rook:

#so below it will say the operator(aka rook in this case), and then the gun it chose from his load out

 print('Rook',random.choice(Rook))

how do i make what guns it chooses from depend on what got printed out above it.. what code would it be and if possible could i have an explanation

  • Possible duplicate of [How do I create a variable number of variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables) – Aran-Fey Mar 27 '18 at 22:58
  • 3
    Instead of using variables like `Rook`, `Doc` and `Mute`, store those values in a dict. – Aran-Fey Mar 27 '18 at 22:59

1 Answers1

4

Try using a dictionary instead:

import random

defenders_and_weapons = {'Rook': ['MP5', 'P90', 'SG-CQB'], 
                         'Doc': ['MP5', 'P90', 'SG-CQB'], 
                         'Mute': ['MP5K', 'M590A1'],
                         'Smoke': ['FMG-9', 'M590A1']
                         # Add more operators using the same format
                         }

defender = random.choice(list(defenders_and_weapons.keys()))
weapon = random.choice(defenders_and_weapons[defender])

print('defender: {}, weapon: {}'.format(defender, weapon))
aL_eX
  • 1,453
  • 2
  • 15
  • 30
  • 1
    Thank you sooo much.. i will look more into using this in future programs. this was my first time using this site and im new to python thanks for your help. now i can work on adding a button onto the GPIO pins of my pi and making it give me a random operator when i press the button – Kyle Hawkins Mar 28 '18 at 00:23
  • https://meta.stackoverflow.com/questions/367446/mass-trivial-tag-only-edits – Hans Passant May 07 '18 at 07:50