[Tutor] role playing game - help needed
Alan Gauld
alan.gauld at btinternet.com
Thu Dec 9 01:35:32 CET 2010
"Al Stern" <albstern at gmail.com> wrote
> 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,
Some things to think about below.
> attributes = {"strength": 0, "health": 0, "wisdom": 0, "dexterity":
> 0}
> MAX_POINTS = int(30)
> keys = attributes.keys()
> values = attributes.values()
you don't need keys and values as variables because you can always
fetch them from atttributes as needed.
> list (values)
This converts values into a list, which it already is, then throws it
away.
I suspect you think its doing something else?.
> 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?: "))
One of the advantages of using a dict is that you can use a loop here
and thus easily extend your data in the futire without changing the
input/output code:
for name in attributes.keys():
attributes[name] = int( input("How many points do you want to
assign to %s " % name) )
> #point allocation
> point_total = 0
> for val in values:
> point_total += val
point_total = sum( attributes.values() )
> 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"]))
And as above this can be a loop:
for name in attributes.keys():
print("\n", name, ":", attributes[name] )
> available_points = (MAX_POINTS) - (point_total)
No need for any of the parentheses here
> while point_total != "":
You are comparing point_total to a string, but point_total is a
number.
You need to be careful about keeping your data types consistent.
> if point_total > 30:
> print ("\nYou have gone over your alloted 30 points.")
> print ("Please re-enter your choices. ")
....
>
> #point allocation
> point_total = 0
> for val in values:
> point_total += val
again, use sum()
> 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
The logic here could be simplified into
while point_total > 30:
> 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 != "":
Again comparing to a string but you have made choice an integer
> 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
Note, you haven't actually assigned anything to values since the user
entered the new data. This is another reason it would be better to not
have the variable, just get values directly from attributes on demand.
HTH,
--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list