[Tutor] role playing game - help needed

Peter Otten __peter__ at web.de
Tue Dec 7 17:06:22 CET 2010


Al Stern wrote:

> Apologies for all my questions.  Up to this point I have been able to work
> out most of the challenges but I seem to have hit a wall.  Can't seem to
> make any progress and completely frustrated.
> 
> I looked at the 11/21 discussion.  From the documentation, I realized I
> needed to set the variables to view the keys and values.  Getting an error
> though.
> 
> attributes = {"strength": 0, "health": 0, "wisdom": 0, "dexterity": 0}
> MAX_POINTS = 30
> keys = attributes.viewkeys()
> values = attributes.viewvalues()
> 
> Traceback (most recent call last):
>   File "C:\Users\Public\Documents\My Python
>   programs\role_playing_game1.py",
> line 8, in <module>
>     keys = attributes.viewkeys()
> AttributeError: 'dict' object has no attribute 'viewkeys'

The dictionary methods you are looking for are called keys() and values() 
not viewkeys() or viewvalues(). They do return view objects which may be 
causing the confusion. Have a look at the documentation at

http://docs.python.org/dev/py3k/library/stdtypes.html#dictionary-view-
objects

which shows a simple example. 
By the way you, can use the interactive interpreter to find out what 
attributes an object has to offer:

$ python3
Python 3.1.1+ (r311:74480, Nov  2 2009, 15:45:00)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> d = {"a": 1, "b": 2}
>>> dir(d)
['__class__', '__contains__', '__delattr__', '__delitem__', '__doc__', 
'__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', 
'__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', 
'__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', 
'__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 
'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 
'setdefault', 'update', 'values']

Peter



More information about the Tutor mailing list