<div>Alan</div>
<div> </div>
<div>Many thanks, that is really useful.</div>
<div> </div>
<div>I will go through this a bit at a time over the next few days to ensure I understand what I am doing!</div>
<div> </div>
<div>I think functions come in the next chapter!</div>
<div> </div>
<div>Jon<br><br> </div>
<div><span class="gmail_quote">On 25/01/06, <b class="gmail_sendername">Alan Gauld</b> <<a href="mailto:alan.gauld@freenet.co.uk">alan.gauld@freenet.co.uk</a>> wrote:</span>
<blockquote class="gmail_quote" style="PADDING-LEFT: 1ex; MARGIN: 0px 0px 0px 0.8ex; BORDER-LEFT: #ccc 1px solid">Hi Jon,<br><br>> 1. I am sure I have written far more code than required.<br>> Where could I have made some shorcuts?
<br><br>Probably but shortcuts are not necesasarily a good thing<br>if they obscure readability...<br><br>However one area that would clean it up a bit is if you were to<br>use a dictionary rather than lists for the attributes(see below):
<br><br>> # Set max number of available points<br>> POINTS_POOL = 30<br><br>Thus duplicates available_points below. You only really need it once...<br>although I see that you modify avail... Might be better to do it by
<br>assigning this constant as the initial value of available_points:<br><br>available_points = POINTS_POOL<br><br>> # store attribute values<br>> attributes = [["Strength", 0], ["Health", 0],<br>
> ["Wisdom", 0], ["Dexterity",0]]<br><br>Use a dictionary here instead:<br><br>attributes = {'Strength': 0, 'Health':0, 'Wisdom':0, Dexterity:0}<br><br>>strength = attributes[0]
<br>>health = attributes[1]<br>>wisdom = attributes[2]<br>>dexterity = attributes[3]<br><br>and you shouldn't need these now....<br><br><br>available_points = 30<br>used_points = 0<br>attribute_value = ""
<br><br>choice = None<br>while choice != "0":<br> print """<br> -----------------<br> """<br> choice = raw_input("Choice: ")<br><br> # show attributes option
<br>> elif choice == "1":<br>> print "Your attributes are as follows:\n"<br>> print "\t",strength[0], strength[1]<br><br> print '\t Strength', attributes['Strength']
<br><br>is easier to read I think.<br><br> # edit attributes option<br>> elif choice == "2":<br>> # set strength attribute<br>> print "\nYou have",strength[1] + available_points,"points
<br>> available."<br><br>print '\n You have', attributes['Strength'] + available_points,'points'<br><br>Although I personally prefer to use string formatting:<br><br>print '\n You have %d points', (attributes['Strength'] + available_points)
<br><br>> if attribute_value > (strength[1] + available_points):<br><br>Since you do the sum twice I'd store the value up top<br><br>avail_strength = attributes['Strength'] + available_points<br><br>And use avail_strength in both the print and comparison.
<br><br>> strength[1] = attribute_value<br>> used_points = strength[1] + health[1] + wisdom[1] + dexterity[1]<br><br>You can use the sum() function here:<br><br>used_points = sum(attributes.values())<br>
<br><br>Also since you are repeating almost exactly the same<br>code for each attribute you coiuld create a function that<br>takes the attribute name as a parameter.<br>Have you come across functions yet? If not don't worry
<br>this approach works it just means more typing and<br>multiple changes to fix things.<br><br>Hope those points help a little.<br><br>Alan G<br>Author of the learn to program web tutor<br><a href="http://www.freenetpages.co.uk/hp/alan.gauld">
http://www.freenetpages.co.uk/hp/alan.gauld</a><br><br><br></blockquote></div><br><br clear="all"><br>-- <br>Best Regards<br><br>Jon Moore