Re: [Tutor] role playing game - help needed

2010-12-06 Thread Al Stern
Thanks for the advice.  I think I have the dictionary function set up right
now although I'm still not clear why it is better than the list.

attributes = {"strength": 0, "health": 0, "wisdom": 0, "dexterity": 0}

I think my next task is to set up a while function based on when
available_points drops below 0.  A little lost on how to do it though.

-Original Message-
From: tutor-bounces+alans=risingrealty@python.org
[mailto:tutor-bounces+alans=risingrealty@python.org] On Behalf Of Robert
Sjöblom
Sent: Monday, December 06, 2010 8:04 AM
To: tutor@python.org
Subject: Re: [Tutor] role playing game - help needed

>I am starting with a book called Python Programming for the Absolute
Beginner by Michael Dawson.  The book has been >pretty good >and up to this
point, I have grasped all the concepts it has covered.  At the end of each
chapter, there are a number of challenges you >need to complete before
moving on.  Problem is, I have gotten stumped on one in Chapter 5: Lists and
Dictionaries.
[snip]
> attributes = ["strength", "health", "wisdom", "dexterity"]
> points = [0,0,0,0]
> MAX_POINTS = 30
> available_points = MAX_POINTS - sum(points)

Before anyone comments that you can write a function for the points
system, which was helpful to me when I asked a question regarding that
very chapter: The book doesn't deal with functions until Chapter 6.

As for your problem, Alan; I believe Alan already answered it -- you
have two different lists, attributes and points, but a dictionary
would be easier to handle. Not to say that it couldn't be done the way
you're doing it though. A dictionary is built with key:value pairs.
Then it's just about figuring out a way to change the value associated
with each key when the attribute changes. I don't want to spoil the
challenge of working it out yourself, but when I asked I was told to
check out how values() and sum() worked.

best regards,
Robert S.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] role playing game - help needed

2010-12-06 Thread Al Stern
Ok.  I think I am starting to get it but still not sure how to separate the
value from the key.  Once I have this...

attributes = {"strength": 0, "health": 0, "wisdom": 0, "dexterity": 0}
MAX_POINTS = 30

How do I set the variable for available_points?

available_points = MAX_POINTS - (not sure what goes here)

I am pretty sure I will change the values by something like this...

attributes["strength"] = input("\nHow many points do you want to assign to
strength?: ")

Please let me know if this isn't advisable.  It seems to work on the
surface.

On Mon, Dec 6, 2010 at 1:16 PM, Alan Gauld wrote:

>
> "Al Stern"  wrote
>
>
> Thanks for the advice.  I think I have the dictionary function set up right
>> now although I'm still not clear why it is better than the list.
>>
>> attributes = {"strength": 0, "health": 0, "wisdom": 0, "dexterity": 0}
>>
>
> Consider where you want to update the points for "health"
>
> Using two lists you need to loop over the keys list to find the index
> of "health" then access the values list to set the points. Something
> like this:
>
> for x in range(len(keys)):
>   if keys[x] == "health":
>   values[x] = newValue
>
> With a dictionary you just need to do
>
> attributes["health"] = newValue
>
> That's a lot less typing and will be faster performance too.
> I'd also say its a lot easier to see whats happening - its more readable.
>
> HTH,
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] role playing game - help needed

2010-12-07 Thread Al Stern
Apologies for all my questions.  Up to this point I have been able to work
out most of the challenges but I seem to have hit a wall.  Can't seem to
make any progress and completely frustrated.

I looked at the 11/21 discussion.  From the documentation, I realized I
needed to set the variables to view the keys and values.  Getting an error
though.

attributes = {"strength": 0, "health": 0, "wisdom": 0, "dexterity": 0}
MAX_POINTS = 30
keys = attributes.viewkeys()
values = attributes.viewvalues()

Traceback (most recent call last):
  File "C:\Users\Public\Documents\My Python programs\role_playing_game1.py",
line 8, in 
keys = attributes.viewkeys()
AttributeError: 'dict' object has no attribute 'viewkeys'




On Tue, Dec 7, 2010 at 3:44 AM, Alan Gauld wrote:

>
> "Al Stern"  wrote
>
>  attributes = {"strength": 0, "health": 0, "wisdom": 0, "dexterity": 0}
>> MAX_POINTS = 30
>>
>> How do I set the variable for available_points?
>>
>> available_points = MAX_POINTS - (not sure what goes here)
>>
>
> Check the mail from Robert Sjoblom, he gives you the necessary clues.
> You can check the archive a few weeks back(21st Nov) for his question
> too and get some alternative options and discussion.
>
>
> attributes["strength"] = input("\nHow many points do you want to assign to
>> strength?: ")
>>
>> Please let me know if this isn't advisable.  It seems to work on the
>> surface.
>>
>
> Close, but remember that input() returns a string. You need numbers
> so you need to convert strings to integers.
>
>
> HTH,
>
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] role playing game - help needed

2010-12-07 Thread Al Stern
Tried to use the documentation but still getting the errors...

The 1st one has to do with the available_points

# set variables
attributes = {"strength": 0, "health": 0, "wisdom": 0, "dexterity": 0}
MAX_POINTS = 30
available_points = MAX_POINTS - attributes.values()
keys = attributes.keys()
values = attributes.values()

this is the error i'm getting...

Traceback (most recent call last):
  File "C:\Users\Public\Documents\My Python programs\role_playing_game1.py",
line 8, in 
available_points = MAX_POINTS - attributes.values()
TypeError: unsupported operand type(s) for -: 'int' and 'dict_values'

I know using attributes.values here isn't correct but I can't figure out how
to put the sum of the values into that equation.

I looked up this part f the docs...
http://docs.python.org/py3k/library/stdtypes.html?highlight=values#dict.values
and tried to copy the format into my program. I am attempting to get the
total of the values of everything in my dictionary.   Not sure what is
different between my 'attributes' dictionary and the 'dishes' dictionary
they use.  I used the following code and got the following error.

attributes["strength"] = input("\nHow many points do you want to assign to
strength?: ")

#point allocation
point_total = 0
for val in values:
  point_total += val
  print (point_total)

and get this error...

Traceback (most recent call last):
  File "C:\Users\Public\Documents\My Python programs\role_playing_game1.py",
line 26, in 
point_total += val
TypeError: unsupported operand type(s) for +=: 'int' and 'str'




On Tue, Dec 7, 2010 at 10:06 AM, Peter Otten <__pete...@web.de> wrote:

> Al Stern wrote:
>
> > Apologies for all my questions.  Up to this point I have been able to
> work
> > out most of the challenges but I seem to have hit a wall.  Can't seem to
> > make any progress and completely frustrated.
> >
> > I looked at the 11/21 discussion.  From the documentation, I realized I
> > needed to set the variables to view the keys and values.  Getting an
> error
> > though.
> >
> > attributes = {"strength": 0, "health": 0, "wisdom": 0, "dexterity": 0}
> > MAX_POINTS = 30
> > keys = attributes.viewkeys()
> > values = attributes.viewvalues()
> >
> > Traceback (most recent call last):
> >   File "C:\Users\Public\Documents\My Python
> >   programs\role_playing_game1.py",
> > line 8, in 
> > keys = attributes.viewkeys()
> > AttributeError: 'dict' object has no attribute 'viewkeys'
>
> The dictionary methods you are looking for are called keys() and values()
> not viewkeys() or viewvalues(). They do return view objects which may be
> causing the confusion. Have a look at the documentation at
>
> http://docs.python.org/dev/py3k/library/stdtypes.html#dictionary-view-
> objects
>
> which shows a simple example.
> By the way you, can use the interactive interpreter to find out what
> attributes an object has to offer:
>
> $ python3
> Python 3.1.1+ (r311:74480, Nov  2 2009, 15:45:00)
> [GCC 4.4.1] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> d = {"a": 1, "b": 2}
> >>> dir(d)
> ['__class__', '__contains__', '__delattr__', '__delitem__', '__doc__',
> '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__',
> '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__',
> '__lt__',
> '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
> '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__',
> 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem',
> 'setdefault', 'update', 'values']
>
> Peter
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] role playing game - help needed

2010-12-08 Thread Al Stern
Peter, Thanks for the advice as to how to use numbers.  I had learned this
before but the example in the documentation didn't use 'int' so I thought I
didn't need to when using dictionary values.

Anyway, I finally finished the program.  I am sure it isn't the most
efficient code and I suspect I should have used the 'while' loop earlier,
but the bottom line is it seems to work.  I will paste what I came up with
in case anyone is interested.

Thanks again to everyone.  I couldn't have done it wothout you.

-Al

# character creator / role playing game
# have 30 total points to work with
# set variables
attributes = {"strength": 0, "health": 0, "wisdom": 0, "dexterity": 0}
MAX_POINTS = int(30)
keys = attributes.keys()
values = attributes.values()

list (values)
print("""
Welcome to Quest.  The goal of the game is to help our hero achieve his
mission.
He will need strength, health, wisdom and dexterity to survive.
You will have 30 points to 'spend' on these attributes.
Use them wisely.  His life depends on it.
"""
)

attributes["strength"] = int(input("\nHow many points do you want to assign
to strength?: "))
attributes["health"] = int(input("\nHow many points do you want to assign to
health?: "))
attributes["wisdom"] = int(input("\nHow many points do you want to assign to
wisdom?: "))
attributes["dexterity"] = int(input("\nHow many points do you want to assign
to dexterity?: "))

#point allocation
point_total = 0
for val in values:
  point_total += val
print ("\nThis is how you have chosen to allocate your 30 points.")
print ("\nStrength:",(attributes["strength"]))
print ("Health:", (attributes["health"]))
print ("Wisdom:", (attributes["wisdom"]))
print ("Dexterity:", (attributes["dexterity"]))
available_points = (MAX_POINTS) - (point_total)
while point_total != "":
if point_total > 30:
  print ("\nYou have gone over your alloted 30 points.")
  print ("Please re-enter your choices. ")
  attributes["strength"] = int(input("\nHow many points do you want to
assign to strength?: "))
  attributes["health"] = int(input("\nHow many points do you want to
assign to health?: "))
  attributes["wisdom"] = int(input("\nHow many points do you want to
assign to wisdom?: "))
  attributes["dexterity"] = int(input("\nHow many points do you want to
assign to dexterity?: "))

  #point allocation
  point_total = 0
  for val in values:
point_total += val
  print ("\nThis is how you have chosen to allocate your 30 points.")
  print ("\nStrength:",(attributes["strength"]))
  print ("Health:", (attributes["health"]))
  print ("Wisdom:", (attributes["wisdom"]))
  print ("Dexterity:", (attributes["dexterity"]))
  available_points = (MAX_POINTS) - (point_total)
  continue
else:
  break
print ("\nYou have", available_points, "points left.")
print ("\nSince you have points left over, you may reallocate your points or
begin your quest.")
choice = int(input("\nTo reallocate, press 1.  To begin, press 2: "))
while choice != "":
  if choice == 1:
print ("Please re-enter your choices. ")
attributes["strength"] = int(input("\nHow many points do you want to
assign to strength?: "))
attributes["health"] = int(input("\nHow many points do you want to
assign to health?: "))
attributes["wisdom"] = int(input("\nHow many points do you want to
assign to wisdom?: "))
attributes["dexterity"] = int(input("\nHow many points do you want to
assign to dexterity?: "))

#point allocation
point_total = 0
for val in values:
  point_total += val
print ("\nThis is how you have chosen to allocate your 30 points.")
print ("\nStrength:",(attributes["strength"]))
print ("Health:", (attributes["health"]))
print ("Wisdom:", (attributes["wisdom"]))
print ("Dexterity:", (attributes["dexterity"]))
available_points = (MAX_POINTS) - (point_total)
print ("\nYou have", available_points, "points left.")
print ("\nSince you have points left over, you may reallocate your
points or begin your quest.")
choice = int(input("\nTo reallocate, press 1.  To begin, press 2: "))
  elif choice == 2:
break
  else:
continue


print ("You are now ready to begin your quest.  Good 

[Tutor] Dictionaries - Using 1 Key:Value to find another

2010-12-11 Thread Al Stern
This was another execise in my book.  Following is my code for a program
that uses dictionaries to find and edit pairs of fathers and sons.  The
program works right up to the final step which is to find out if any given
father is actually a grandfather to someone else in the dictionary.  I set
up multiple generations in the dictionary so there would be available
matches.

# father son "who's your daddy" program
#create a dictionary of father son pairs.  Include multiple generations.
pairs = {"Joel" : "Al",
 "Bill" : "Joel",
 "Joseph" : "Bill",
  "Palmer" : "Bob",
 "Bob" : "Will",
 "Al" : "Royal"}
fathers = pairs.keys()
sons = pairs.values()
choice = None
while choice != "0":

print("""
Welcome to Who's Your Daddy? A mini database of fathers and sons.
When entering names, please capitilize the 1st letter of each name.

0 - Exit
1 - List all Father-Son Pairs
2 - List all Fathers
3 - Look Up Father-Son Pairs
4 - Add a Father-Son Pair
5 - Delete a Father-Son Pair
6 - Replace the Son of a Father-Son Pair
7 - Replace a Father-Son Pair
8 - Look up Grandfather_Father_Son Pairs
""")

choice = input("Choice: ")


# To end the program
if choice == "0":
print ("Thanks for playing.  Goodbye.")
# List pairs
elif choice == "1":
print ("List of all Father and Son pairs: ")
print (pairs)
elif choice == "2":
print ("List of all Fathers: ")
for fathers in pairs:
print (fathers)
elif choice == "3":
print ("Look up Father-Son pairs: ")
father = input ("What is the name of the father in the pair you want
to look up? ")
if father in pairs:
son = pairs[father]
print ("\n", father, "is the father of",son, ".")
else:
print ("\nSorry.  That father does not exist.")
elif choice == "4":
print ("Add a Father-Son pair: ")
new_father = input ("What is the name of the father you wish to add?
")
if new_father not in pairs:
new_son = input ("What is the name ofthe son you wish to add? ")
pairs[new_father] = new_son
print ("\n", new_father, "has been added.")
else:
print ("\nThat Father-Son pair already exists.  Try again.")

elif choice == "5":
print ("Delete a Father-Son pair: ")
del_father = input ("What is the name of the father in the pair you
wish to delete? ")
if del_father in pairs:
del pairs[del_father]
print ("\nThe pair in which", del_father, "is the father has
been deleted.")
else:
print ("\nSorry.", del_father, "is not a valid father name.")
elif choice == "6":
print ("Replace the son of a Father-Son Pair: ")
son_old = input ("What is the name of the father whose son you wish
to replace? ")
if son_old in pairs:
son_new = input ("What is the name of the new son in this pair?
")
pairs[son_old] = son_new
print (son_new, "has replaced", son_old, "in the list.")
else:
print ("Sorry, that son does not exist.  Try again.")
elif choice == "7":
print ("Replace a Father-Son pair: ")
father = input ("Who is the father in the Father-Son pair do you
which to replace? ")
if father in pairs:
father = input("What is the name of the father in the new
Father-Son pair? ")
son = input("What is the name of the son in the new Father-Son
pair? ")
pairs[father] = son
print ("\nThe new pair of", father, "and", son, "have been added
to the list.")

else:
print ("Sorry, that father does not exist.  Try again.")
elif choice == "8":
print ("Find the grandson of a person in the list.")
grandfather = input("Which father do you want to look up to see if
he is a grandfather? ")
if grandfather in pairs:
father == pairs[grandfather]
for father in pairs:
grandson == pairs[father]
print (grandfather, "is the grandfather of", grandson, ".")
else:
print ("Sorry.", grandfather, "is not a grandfather.  Try
again.")



input ("\n\nPress the enter key to exit.\n")

Choice 8 gives me the following error...

Traceback (most recent call last):
  File "C:\Users\Public\Documents\My Python programs\father_son.py", line
105, in 
father == pairs[grandfather]
NameError: name 'father' is not defined

I thought father got defined in the

father == pairs[grandfather]
line.  I have tried it a couple different ways but always get the father is
not defined error once I enter the name.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mail

Re: [Tutor] Dictionaries - Using 1 Key:Value to find another

2010-12-11 Thread Al Stern
Ah.  Turns out I was just sloppy and/or stupid.  I realized I had another
error once I fixed the '==' part.

My new code which seems to work is...

elif choice == "8":
print ("Find the grandson of a person in the list.")
grandfather = input("Which father do you want to look up to see if
he is a grandfather? ")
if grandfather in pairs:
father = pairs[grandfather]
grandson = pairs[father] 
print (grandfather, "is the grandfather of", grandson, ".")
else:
print ("\nSorry.", grandfather, "is not a grandfather.  Try
again.")

Thanks all.
_
 
www.electronsbaseball.com

-Original Message-
From: David [mailto:bouncingc...@gmail.com] 
Sent: Saturday, December 11, 2010 6:35 PM
To: Al Stern
Cc: tutor@python.org
Subject: Re: [Tutor] Dictionaries - Using 1 Key:Value to find another

On 12 December 2010 11:10, Al Stern  wrote:
>
> I thought father got defined in the
>
> father == pairs[grandfather]
> line.  I have tried it a couple different ways but always get the father
is
> not defined error once I enter the name.

I only glanced at your code, but maybe you have some typos there:

father == pairs[grandfather]
for father in pairs:
grandson == pairs[father]

should probably be
father = pairs[grandfather]
for father in pairs:
grandson = pairs[father]

Try that.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor