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

Jon Moore jonathan.r.moore at gmail.com
Wed Jan 25 17:44:26 CET 2006


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]]
strength = attributes[0]
health = attributes[1]
wisdom = attributes[2]
dexterity = attributes[3]
available_points = 30
used_points = 0
attribute_value = ""

while choice != "0":
    print \
          """
          -----------------
          Character Creator
          -----------------

          0 - Exit
          1 - Show Character Attributes
          2 - Set Character Attributes
          3 - Reset Character Attributes

          """
    choice = raw_input("Choice: ")
    print

    # exit option
    if choice == "0":
        print "Good bye."

    # show attributes option
    elif choice == "1":
        print "Your attributes are as follows:\n"
        print "\t",strength[0], strength[1]
        print "\t",health[0], health[1]
        print "\t",wisdom[0], wisdom[1]
        print "\t",dexterity[0], dexterity[1]
        print "\nYou have",available_points,"points available."

    # edit attributes option
    elif choice == "2":
        # set strength attribute
        print "\nYou have",strength[1] + available_points,"points
available."
        attribute_value = int(raw_input("How much would you like to assign
to strength?: "))
        if attribute_value > (strength[1] + available_points):
            print "Error - You do not have that many points available!\n"
            print "Please reset the character or assign a value equal\nto or
lower than the avaialble points."
            continue
        strength[1] = attribute_value
        used_points = strength[1] + health[1] + wisdom[1] + dexterity[1]
        available_points = POINTS_POOL - used_points

        # set health attribute
        print "\nYou have",health[1] + available_points,"points available."
        attribute_value = int(raw_input("How much would you like to assign
to health?: "))
        if attribute_value > (health[1] + available_points):
            print "Error - You do not have that many points available!\n"
            print "Please reset the character or assign a value equal\nto or
lower than the avaialble points."
            continue
        health[1] = attribute_value
        used_points = strength[1] + health[1] + wisdom[1] + dexterity[1]
        available_points = POINTS_POOL - used_points

        # set wisdom attribute
        print "\nYou have",wisdom[1] + available_points,"points available."
        attribute_value = int(raw_input("How much would you like to assign
to wisdom?: "))
        if attribute_value > (wisdom[1] + available_points):
            print "Error - You do not have that many points available!\n"
            print "Please reset the character or assign a value equal\nto or
lower than the avaialble points."
            continue
        wisdom[1] = attribute_value
        used_points = strength[1] + health[1] + wisdom[1] + dexterity[1]
        available_points = POINTS_POOL - used_points

        # set dexterity attribute
        print "\nYou have",dexterity[1] + available_points,"points
available."
        attribute_value = int(raw_input("How much would you like to assign
to dexterity?: "))
        if attribute_value > (dexterity[1] + available_points):
            print "Error - You do not have that many points available!\n"
            print "Please reset the character or assign a value equal\nto or
lower than the avaialble points."
            continue
        dexterity[1] = attribute_value
        used_points = strength[1] + health[1] + wisdom[1] + dexterity[1]
        available_points = POINTS_POOL - used_points

        # print amount of unused points
        print "\nYou have",available_points,"unused points."

    # reset attributes option
    elif choice == "3":
        strength[1] = 0
        health[1] = 0
        wisdom[1] = 0
        dexterity[1] = 0
        used_points = strength[1] + health[1] + wisdom[1] + dexterity[1]
        available_points = POINTS_POOL - used_points
        print "Attributes have been reset."

    # some unknown choice
    else:
        print "Sorry, but" ,choice, "is not a valid choice."

--
Best Regards

Jon Moore
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060125/77422c42/attachment-0001.htm 


More information about the Tutor mailing list