Re: [Tutor] Controling my loops and redundant code?!?

2006-01-27 Thread Jon Moore
PaulThe book is called 'Python Programming for the absolute beginner'. It is written by Michael Dawson and published by Premier Press.I have to say that as someone that has no experience in programming what so ever, I am hooked! I need to learn Python for a job I am starting next month and to say I felt a little worried at the idea is an understatement. Since going through the first few chapters of this book (and having the support of this group) I can not wait to learn more!
JonOn 26/01/06, Paul Kraus <[EMAIL PROTECTED]> wrote:
What book are you working through? That is a pretty interesting exercise.PaulOn Thursday 26 January 2006 12:52 pm, Bob Gailer wrote:> At 08:44 AM 1/25/2006, Jon Moore wrote:>> Hi,>
> I have written the program below as an exercise from a book I am working my> way through.>> Objective from book:> Write a character creator program for a role-playing-game. The player> should be given a pool of 30 points to spend on four attributes: strength,
> health, wisdom and dexterity. The player should be able to spend points> from the pool on any attribute and should be also be able to take points> from an attribute and put them back in the pool.
>> Although the program meets the aim of the exercise set out in the book ,> there are a couple of things I am not happy with!>> 1. I am sure I have written far more code than required. Where could I have
> made some shorcuts?>> 2. Should the user enter a value greater than what is available, the> program kicks the user all the way back to the main menu. How could I tidy> this up to just loop round to ask the user to try a new value?
>> choice = None>> # Set max number of available points> POINTS_POOL = 30>> # store attribute values> attributes = [["Strength", 0], ["Health", 0], ["Wisdom", 0], ["Dexterity",
> 0]]>>> Some ideas to chew on as you develop skills and understanding of> programming.> Separate the "essential data" from the code. The essential data are> attributes, associated points and max_points. So
>> attributes = ["Strength", "Health", "Wisdom", "Dexterity"]> points = [0, 0, 0, 0]> MAX_POINTS = 30>> In this model the relationship between attributes and points is by
> position. Later you will study and use classes; then the points list will> be replaced by a list of class instances, one per attribute.>> Develop code that operates on these lists to accomplish the various
> objectives. The code itself will never refer to any attribute by name!>> For example - to list the attributes with their points:>> for x in range(len(attributes)):>   print "\t",attributes[x], points[x]
>> To assign values. Note I separated getting input from converting it to> integer so we can see if the user's entry is convertible.:>> available_points = MAX_POINTS - sum(points)> print "You have " + available_points + " available."
> for x in range(len(attributes)):>  cvalue = raw_input("How much would you like to assign to " + attributes[x]> + " ?: "))>   if cvalue.isdigit():> value = int(cvalue)
>   else:> print "Number expected">> [snip]> --> Bob Gailer> 510-978-4454--Paul Kraus=-=-=-=-=-=-=-=-=-=-=PEL Supply CompanyNetwork Administrator
216.267.5775 Voice216.267.6176 Faxwww.pelsupply.com=-=-=-=-=-=-=-=-=-=-=___Tutor maillist  -  
Tutor@python.orghttp://mail.python.org/mailman/listinfo/tutor-- Best RegardsJon Moore
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Controling my loops and redundant code?!?

2006-01-26 Thread Bob Gailer
At 08:44 AM 1/25/2006, Jon Moore wrote:
> Hi,
>
> I have written the program below as an exercise from a book I am 
> working my way through.
>
> Objective from book:
> Write a character creator program for a role-playing-game. The player 
> should be given a pool of 30 points to spend on four attributes: 
> strength, health, wisdom and dexterity. The player should be able to 
> spend points from the pool on any attribute and should be also be able 
> to take points from an attribute and put them back in the pool.
>
> Although the program meets the aim of the exercise set out in the book 
> , there are a couple of things I am not happy with!
>
> 1. I am sure I have written far more code than required. Where could I 
> have made some shorcuts?
>
> 2. Should the user enter a value greater than what is available, the 
> program kicks the user all the way back to the main menu. How could I 
> tidy this up to just loop round to ask the user to try a new value?
>
> choice = None
>
> # Set max number of available points
> POINTS_POOL = 30
>
> # store attribute values
> attributes = [["Strength", 0], ["Health", 0], ["Wisdom", 0], 
> ["Dexterity", 0]] 

Some ideas to chew on as you develop skills and understanding of 
programming.
Separate the "essential data" from the code. The essential data are 
attributes, associated points and max_points. So

attributes = ["Strength", "Health", "Wisdom", "Dexterity"]
points = [0, 0, 0, 0]
MAX_POINTS = 30

In this model the relationship between attributes and points is by 
position. Later you will study and use classes; then the points list 
will be replaced by a list of class instances, one per attribute.

Develop code that operates on these lists to accomplish the various 
objectives. The code itself will never refer to any attribute by name!
 
For example - to list the attributes with their points:

for x in range(len(attributes)):
  print "\t",attributes[x], points[x]

To assign values. Note I separated getting input from converting it to 
integer so we can see if the user's entry is convertible.:

available_points = MAX_POINTS - sum(points)
print "You have " + available_points + " available."
for x in range(len(attributes)):
 cvalue = raw_input("How much would you like to assign to " + 
attributes[x] + " ?: "))
  if cvalue.isdigit():
value = int(cvalue)
  else:
print "Number expected"

[snip]
-- 
Bob Gailer
510-978-4454
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Controling my loops and redundant code?!?

2006-01-26 Thread Paul Kraus
What book are you working through? That is a pretty interesting exercise.

Paul
On Thursday 26 January 2006 12:52 pm, Bob Gailer wrote:
> At 08:44 AM 1/25/2006, Jon Moore wrote:
>
> Hi,
>
> I have written the program below as an exercise from a book I am working my
> way through.
>
> Objective from book:
> Write a character creator program for a role-playing-game. The player
> should be given a pool of 30 points to spend on four attributes: strength,
> health, wisdom and dexterity. The player should be able to spend points
> from the pool on any attribute and should be also be able to take points
> from an attribute and put them back in the pool.
>
> Although the program meets the aim of the exercise set out in the book ,
> there are a couple of things I am not happy with!
>
> 1. I am sure I have written far more code than required. Where could I have
> made some shorcuts?
>
> 2. Should the user enter a value greater than what is available, the
> program kicks the user all the way back to the main menu. How could I tidy
> this up to just loop round to ask the user to try a new value?
>
> choice = None
>
> # Set max number of available points
> POINTS_POOL = 30
>
> # store attribute values
> attributes = [["Strength", 0], ["Health", 0], ["Wisdom", 0], ["Dexterity",
> 0]]
>
>
> Some ideas to chew on as you develop skills and understanding of
> programming.
> Separate the "essential data" from the code. The essential data are
> attributes, associated points and max_points. So
>
> attributes = ["Strength", "Health", "Wisdom", "Dexterity"]
> points = [0, 0, 0, 0]
> MAX_POINTS = 30
>
> In this model the relationship between attributes and points is by
> position. Later you will study and use classes; then the points list will
> be replaced by a list of class instances, one per attribute.
>
> Develop code that operates on these lists to accomplish the various
> objectives. The code itself will never refer to any attribute by name!
>
> For example - to list the attributes with their points:
>
> for x in range(len(attributes)):
>   print "\t",attributes[x], points[x]
>
> To assign values. Note I separated getting input from converting it to
> integer so we can see if the user's entry is convertible.:
>
> available_points = MAX_POINTS - sum(points)
> print "You have " + available_points + " available."
> for x in range(len(attributes)):
>  cvalue = raw_input("How much would you like to assign to " + attributes[x]
> + " ?: "))
>   if cvalue.isdigit():
> value = int(cvalue)
>   else:
> print "Number expected"
>
> [snip]
> --
> Bob Gailer
> 510-978-4454

-- 
Paul Kraus
=-=-=-=-=-=-=-=-=-=-=
PEL Supply Company
Network Administrator
216.267.5775 Voice
216.267.6176 Fax
www.pelsupply.com
=-=-=-=-=-=-=-=-=-=-=
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Controling my loops and redundant code?!?

2006-01-26 Thread Bob Gailer
At 08:44 AM 1/25/2006, Jon Moore wrote:

Hi,
I have written the program below as an exercise from a book I am working my way through.
Objective from book:Write
a character creator program for a role-playing-game. The player should
be given a pool of 30 points to spend on four attributes: strength,
health, wisdom and dexterity. The player should be able to spend points
from the pool on any attribute and should be also be able to take
points from an attribute and put them back in the pool. 
Although the program meets the aim of the exercise set out in the book , there are a couple of things I am not happy with!
1. I am sure I have written far more code than required. Where could I have made some shorcuts? 
2. Should the user enter a value greater than what is available, the
program kicks the user all the way back to the main menu. How could I
tidy this up to just loop round to ask the user to try a new value?
choice = None
# Set max number of available points
POINTS_POOL = 30
# store attribute values
attributes = [["Strength", 0], ["Health", 0], ["Wisdom", 0], ["Dexterity", 0]] 


Some ideas to chew on as you develop skills and understanding of programming.

Separate the "essential data" from the code. The essential data are attributes, associated points and max_points. So


attributes = ["Strength", "Health", "Wisdom", "Dexterity"] 

points = [0, 0, 0, 0]

MAX_POINTS = 30

In this model the relationship between attributes and points is by
position. Later you will study and use classes; then the points list
will be replaced by a list of class instances, one per attribute.

Develop code that operates on these lists to accomplish the various
objectives. The code itself will never refer to any attribute by name! 

 

For example - to list the attributes with their points:


for x in range(len(attributes)):

  print "\t",attributes[x], points[x]

To assign values. Note I separated getting input from converting it to
integer so we can see if the user's entry is convertible.:


available_points = MAX_POINTS - sum(points)

print "You have " + available_points + " available."

for x in range(len(attributes)):

 cvalue = raw_input("How much would you like to assign to " + attributes[x] + " ?: "))

  if cvalue.isdigit():

    value = int(cvalue)

  else:

    print "Number expected"

[snip]-- Bob Gailer510-978-4454
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Controling my loops and redundant code?!?

2006-01-25 Thread Jon Moore
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 thingif they obscure readability...However one area that would clean it up a bit is if you were touse a dictionary rather than lists for the attributes(see below):
> # Set max number of available points> POINTS_POOL = 30Thus 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 nowavailable_points = 30used_points = 0attribute_value = ""
choice = Nonewhile 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 topavail_strength = attributes['Strength'] + available_pointsAnd 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 samecode for each attribute you coiuld create a function thattakes 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 andmultiple changes to fix things.Hope those points help a little.Alan GAuthor of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld-- Best RegardsJon Moore 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Controling my loops and redundant code?!?

2006-01-25 Thread Alan Gauld
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


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor