[Tutor] Using a list

David david at abbottdavid.com
Sat May 30 23:18:22 CEST 2009


David wrote:
> Alan Gauld wrote:
>> "Doug Reid" <rnrcreid at yahoo.com> wrote
>>
>>> The tutorial I'm using is discussing list, tuples, and dictionaries.
>>> ...
>>> four attributes: Strength,Stamina, Wisdom, and Dexterity.
>>> The player should be able to spend points from the pool on
>>> any attribute and should also be able to take points from
>>> an attribute and put them back into the pool.
>>
>> How would you describe a dictionary?
>> Can you see a way to apply it to the problem?
> 
>>
>> #list of attributes and their values
>> attributes=['dexterity=',dexterity ,'strength=',strength, 
>> 'stamina=',stamina, 'wisdom=',wisdom]
>>
>> It is a list but not of attributes. Its a list containing strings and
>> numbers alternating. The strings consist of an attribute name
>> and an equals sign. But you know about a collection type that
>> stores values against strings and allows you to retrieve those
>> values using the string as a key.
>>
>>
>> #creation loop
>> while creating:
>>
>> choice=menu()
>> if choice=='0':
>>   print '\nThank you for using character creator.'
>>   end=raw_input('\n Press enter to end.')
>>   creating=False
>> elif choice=='1':#prints out list of attributes and their values
>>   for entry in attributes:
>>      print entry,
>>
>> This will print
>>
>> attribute=
>> value
>> attribute=
>> value
>>
>> Not very pretty. But if you use a dictionary:
>>
>> for item in dictionary:
>>    print item, '=', dictionary[item]
>>
>> elif choice=='2':
>> allot=raw_input('''What attribute would you like to change?
>> Enter dex for dexterity, str for strength, etc. ''').lower()
>> if allot=='dex':
>> change=int(raw_input('How many points do you wish to allot? '))
>> attributes[1]=dexterity=+change
>> points=points-change
>> if allot=='str':
>> change=int(raw_input('How many points do you wish to allot? '))
>> attributes[3]=strength=+change
>>
>> And this gets even more messy.
>> Again think how it would look with a dictionary...
>>
> I am also new to Python and never programed before. I use the questions 
> from the Tutor list to learn. I put the attributes into dictionaries and 
>  came up with a little game between the user and the computer. Thank you 
> Doug for the question.
> Here is my attempt;
> http://linuxcrazy.pastebin.com/m31d02824
> 
Duh, I had the counter wrong, here is a better one :)
http://linuxcrazy.pastebin.com/m2dc25067

-- 
Powered by Gentoo GNU/Linux
http://linuxcrazy.com


More information about the Tutor mailing list