<div>Alan</div>
<div>&nbsp;</div>
<div>Many thanks, that is really useful.</div>
<div>&nbsp;</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>&nbsp;</div>
<div>I think functions come in the next chapter!</div>
<div>&nbsp;</div>
<div>Jon<br><br>&nbsp;</div>
<div><span class="gmail_quote">On 25/01/06, <b class="gmail_sendername">Alan Gauld</b> &lt;<a href="mailto:alan.gauld@freenet.co.uk">alan.gauld@freenet.co.uk</a>&gt; 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>&gt; 1. I am sure I have written far more code than required.<br>&gt; 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>&gt; # Set max number of available points<br>&gt; 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>&gt; # store attribute values<br>&gt; attributes = [[&quot;Strength&quot;, 0], [&quot;Health&quot;, 0],<br>
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[&quot;Wisdom&quot;, 0], [&quot;Dexterity&quot;,0]]<br><br>Use a dictionary here instead:<br><br>attributes = {'Strength': 0, 'Health':0, 'Wisdom':0, Dexterity:0}<br><br>&gt;strength = attributes[0]
<br>&gt;health = attributes[1]<br>&gt;wisdom = attributes[2]<br>&gt;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 = &quot;&quot;
<br><br>choice = None<br>while choice != &quot;0&quot;:<br>&nbsp;&nbsp; print &quot;&quot;&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -----------------<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;&quot;&quot;<br>&nbsp;&nbsp; choice = raw_input(&quot;Choice: &quot;)<br><br>&nbsp;&nbsp; # show attributes option
<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;elif choice == &quot;1&quot;:<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print &quot;Your attributes are as follows:\n&quot;<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print &quot;\t&quot;,strength[0], strength[1]<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print '\t Strength', attributes['Strength']
<br><br>is easier to read I think.<br><br>&nbsp;&nbsp; # edit attributes option<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;elif choice == &quot;2&quot;:<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# set strength attribute<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print &quot;\nYou have&quot;,strength[1] + available_points,&quot;points
<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; available.&quot;<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>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if attribute_value &gt; (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>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strength[1] = attribute_value<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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