trouble with function using dictionary

This is the place for queries that don't fit in any of the other categories.

trouble with function using dictionary

Postby Wommbatt » Thu Jun 13, 2013 3:43 pm

First off, this is my (obviously) first attempt to put into code a game I've worked on in other mediums, in a language I just started to learn. Got through a couple chapters of a learn python book and wanted to try this idea out.

Code: Select all
creature1 = {'Name': 'Skeleton', 'Level': 1, 'Hp': 4, 'Defense': 3, 'Attack': 9}
creature2 = {'Name': 'Minotaur', 'Level': 1, 'Hp': 4, 'Defense': 3, 'Attack': 9}
creature3 = {'Name': 'Ghoul', 'Level': 1, 'Hp': 4, 'Defense': 3, 'Attack': 9}

def mob_stats(mob):

So I've made 3 dictionaries that hold some basic stats for mobs in the game, and I made a function to display the stats.
My thinking was that if you put the dictionary name into the mob_stats() function it would then spit out: dictionary name, and Name value.
Unfortunately I keep getting:

Code: Select all
>>> mob_stats('creature2')
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
  File "C:/Users/PC/Documents/python/python stuffs/test", line 7, in mob_stats
TypeError: string indices must be integers

What I am really confused about is that inside the python shell if i type:
Code: Select all
>>> mob = creature2
>>> mob['Name']

It works just fine. How come it seems to forget what's going on inside the function?
Am I at least on the right track?
Posts: 24
Joined: Thu Jun 13, 2013 2:15 pm

Re: trouble with function using dictionary

Postby Yoriz » Thu Jun 13, 2013 4:34 pm

You are passing a string
Code: Select all
>>> mob_stats('creature2')

when you should be passing an object
Code: Select all
>>> mob_stats(creature2)

to mob_stats.
New Users, Read This
Join the #python-forum IRC channel on!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython, some Python 3.3
User avatar
Posts: 836
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: trouble with function using dictionary

Postby ochichinyezaboombwa » Thu Jun 13, 2013 4:54 pm

Also, code like
Code: Select all

while you are in the Python interpreter (>>> ), produces some output (which is very confusing),
while in the reality (when you put the same code into a program and run it) it does nothing.
The reason is, mob['Name'] is an object (meaning value of the dictionary's item pointed to by its key 'Name'), and objects don't do anything, they are "nouns". To act, your programs need verbs: print (mob['Name']), delete (mob['Name']), send_to_the_database(mob['Name'])...
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm

Re: trouble with function using dictionary

Postby Wommbatt » Thu Jun 13, 2013 4:58 pm

Haha, I'm both surprised and not surprised at all at how such a small thing makes such a massive difference.
Thank you both for the help and the explanation. 8D
Posts: 24
Joined: Thu Jun 13, 2013 2:15 pm

Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 1 guest