Alan
 
Many thanks, that is really useful.
 
I will go through this a bit at a time over the next few days to ensure I understand what I am doing!
 
I think functions come in the next chapter!
 
Jon

 
On 25/01/06, Alan Gauld <[EMAIL PROTECTED]> wrote:
Hi Jon,

> 1. I am sure I have written far more code than required.
> Where could I have made some shorcuts?

Probably but shortcuts are not necesasarily a good thing
if they obscure readability...

However one area that would clean it up a bit is if you were to
use a dictionary rather than lists for the attributes(see below):

> # Set max number of available points
> POINTS_POOL = 30

Thus duplicates available_points below. You only really need it once...
although I see that you modify avail... Might be better to do it by
assigning this constant as the initial value of available_points:

available_points = POINTS_POOL

> # store attribute values
> attributes = [["Strength", 0], ["Health", 0],
>                    ["Wisdom", 0], ["Dexterity",0]]

Use a dictionary here instead:

attributes = {'Strength': 0, 'Health':0, 'Wisdom':0, Dexterity:0}

>strength = attributes[0]
>health = attributes[1]
>wisdom = attributes[2]
>dexterity = attributes[3]

and you shouldn't need these now....


available_points = 30
used_points = 0
attribute_value = ""

choice = None
while choice != "0":
   print """
         -----------------
         """
   choice = raw_input("Choice: ")

   # show attributes option
>    elif choice == "1":
>         print "Your attributes are as follows:\n"
>        print "\t",strength[0], strength[1]

        print '\t Strength', attributes['Strength']

is easier to read I think.

   # edit attributes option
>    elif choice == "2":
>        # set strength attribute
>         print "\nYou have",strength[1] + available_points,"points
>       available."

print '\n You have', attributes['Strength'] + available_points,'points'

Although I personally prefer to use string formatting:

print '\n You have %d points', (attributes['Strength'] + available_points)

>        if attribute_value > (strength[1] + available_points):

Since you do the sum twice I'd store the value up top

avail_strength = attributes['Strength'] + available_points

And use avail_strength in both the print and comparison.

>        strength[1] = attribute_value
>        used_points = strength[1] + health[1] + wisdom[1] + dexterity[1]

You can use the sum() function here:

used_points = sum(attributes.values())


Also since you are repeating almost exactly the same
code for each attribute you coiuld create a function that
takes the attribute name as a parameter.
Have you come across functions yet? If not don't worry
this approach works it just means more typing and
multiple changes to fix things.

Hope those points help a little.

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld





--
Best Regards

Jon Moore
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to