First of all, forgive my naivete - I'm new to programming and trying to teach myself Python with the aid of the Internet and a book or two.

My current problem that I can't find a solution to is hard for me to explain. First, I'll give the code I want random.choice to look at:

sentbot = {
    'fname':'Sentry Bot',
    'sname':'sentry bot',
   ...
   'state':'calm',
   'attacks': {
          'attack1' : {
                 'attmsg':'The sentry bot fires a pinpoint laser at you.',
                 'mindmg':2,
                 'maxdmg':5,
                 'atype':'laser' },
          'attack2': {
                 'attmsg':'The sentry bot kicks you in the crotch.',
                 'mindmg':1,
                 'maxdmg':3,
                 'atype':'physical' }
                 },
   ...
    }

That's an entry from my critters.py file. From the main game.py file, I want the computer to randomly choose 'attack1' or 'attack2' and be able to use the info form within those sets to determine which attack the sentry bot uses against the player. I can't for the life of me figure out how to do this. Any advice would be greatly appreciated.

Edited 3 Years Ago by Reverend Jim: Fixed formatting

How about this:

atck = sentbot['attacks'][random.choice('attack1', 'attack2')]

or a more portable way

attacks = sentbot['attacks']
atck = attacks[random.choice(attacks.keys())]

Are you by any chance using "Python programming for the absolute beginner (Mike Dawson)"?

How about this:

atck = sentbot['attacks'][random.choice('attack1', 'attack2')]

or a more portable way

attacks = sentbot['attacks']
atck = attacks[random.choice(attacks.keys())]

Are you by any chance using "Python programming for the absolute beginner (Mike Dawson)"?

Thanks for the response. I've tried those before, but I get errors with Random, so I'm not sure if I'm structuring the attack code wrong, or if I've structured the critter code (see my previous post) wrong.

Here's the as-of-yet-incomplete (for testing purposes) critter attack code using your second suggestion, which I prefer so I can put in varying amounts of attacks (attack1, attack2, attack3, ....) for different critters:

for critter in room['occupants']:
		if critter['state'] is 'hostile':
			if critter['curhealth'] > 0:
				attacks = critter['attacks']
				atck = attacks[random.choice(attacks.keys())]
				print(atck['attmsg'])
				print(atck['atype'])

This results in the following error:

Traceback (most recent call last):
File "P:\py3\textgame\game.py", line 414, in <module>
atck = attacks[random.choice(attacks.keys())]
File "C:\Python30\lib\random.py", line 255, in choice
return seq[int(self.random() * len(seq))] # raises IndexError if seq is emp
ty
TypeError: 'dict_keys' object does not support indexing

Using the other suggested code:

for critter in room['occupants']:
		if critter['state'] is 'hostile':
			if critter['curhealth'] > 0:
				atck = critter['attacks'][random.choice('attack1', 'attack2')]
				print(atck['attmsg'])
				print(atck['atype'])

I get the following error:

Traceback (most recent call last):
File "P:\py3\textgame\game.py", line 413, in <module>
atck = critters.sentbot[random.choice('attack1', 'attack2')]
TypeError: choice() takes exactly 2 positional arguments (3 given)

In both cases I tried replacing "critter" with the more direct "critters.sentbot."

The book I have now is "Programming in Python 3 - A Complete Introduction to the Python Language" by Mark Summerfield. I got it because it's specifically for Python 3. I decided I'd start out with 3 so I didn't end up learning an older language version that might die off soon(ish) as far as development and general support.

Something is seriously up with the site. I can't edit my last post. The second example should be:

attacks = sentbot['attacks']
atck = attacks[random.choice([k for k in attacks.keys()])]

Something is seriously up with the site. I can't edit my last post. The second example should be:

attacks = sentbot['attacks']
atck = attacks[random.choice([k for k in attacks.keys()])]

That worked. Thank you very kindly. I owe you a pizza.

I'm not exactly sure why it works, but I'll look that up after I finish up the rest of the critter attack code. Heh.

Regarding site strangeness: It's been very slow for me, and has timed out on multiple occasions since last night.

This question has already been answered. Start a new discussion instead.