2

I just completed an exercise for this text and was wondering if this could be done more efficiently using the same technologies (dictionaries, if statements etc...) This seems to be inefficient to code.

The challenge is as follows: 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 also be able to take points from an attribute and put them back into the pool.

link: http://pastebin.com/PeLLz83e

4
  • 1
    This might be more appropriate for codereview.stackexchange.com Commented Jan 30, 2011 at 22:06
  • Yup. Codereview is a great match for this. Commented Jan 31, 2011 at 10:47
  • user596100: The way to say "thanks" here is to upvote. Just so you know. :) Commented Jan 31, 2011 at 10:48
  • @user596100: When you have recieved a correct answer, please accept it (with the checkmark). Also it's common courtesy to upvote answers you found helpful. Commented Feb 13, 2011 at 7:50

4 Answers 4

3

One thing you could easily improve is, the first series of 'if's:

if pts2 == "1":
    skills["Strength"] += pts
    points -= pts
...

by using a dictionary skills_dict = {"1": "Strength", "2": "Health", ... } you can do:

skills[skills_dict[pts2]] += pts
points -= pts

Same for the second group of 'if's

Sign up to request clarification or add additional context in comments.

Comments

0

Well, one way in which your code seems inelegant is that there is a lot of very similar sequences, specifically:

if rmv2 == "3":
           if rmv < skills["Dextarity"]:
               skills["Dextarity"] -= rmv
               points += rmv
           else:
               print("No")

Where the only thing that varies is what the input was, what stat gets modified, and whether you are adding or removing a value. You could make your code look a little nicer if you found a way to turn that into a reusable component, like with a function.

1 Comment

Cool. Thanks. Can't wait to learn functions. I think that's in a chapter or two.
0

Some comments:

points=int(30)

30 is already an int. Change to

points=30

Also

while player != "e":

Probably should be:

while selection != "exit"

I'd split this program up into loads of small functions, and make a class for the character to store both the skills and the remaining points.

Comments

0

I think you are looking for something like this. Functions are not explained in chapter 5 so I did not include any.

# role playing program
#
# spend 30 points on strenght, health, wisdom, dexterity 
# player can spend and take points from any attribute


# library contains attribute and points
attributes = {"strenght": int("0"),
             "health": "0",
             "wisdom": "0",
             "dexterity": "0"}

pool = int(30)
choice = None
print("The Making of a Hero !!!")
print(attributes)
print("\nYou have", pool, "points to spend.")

while choice != "0":
    # list of choices
    print(
    """
    Options: 

    0 - End
    1 - Add points to an attribute
    2 - remove points from an attribute
    3 - Show attributes
    """
    )
    choice = input("Choose option: ")
    if choice == "0":
        print("\nYour hero stats are:")
        print(attributes)
        print("Good-Bye.")
    elif choice == "1":
        print("\nADD POINTS TO AN ATTRIBUTE")
        print("You have", pool, "points to spend.")
        print(
        """
        Choose an attribute:
           strenght
           health
           wisdom
           dexterity
        """
        )
        at_choice = input("Your choice: ")
        if at_choice.lower() in attributes:
            points = int(input("How many points do you want to assign: "))
            if points <= pool:
                pool -= points
                result = int(attributes[at_choice]) + points
                attributes[at_choice] = result
                print("\nPoints have been added.")
            else:
                print("\nYou do not have that many points to spend")
        else:
            print("\nThat attribute does not exist.")
    elif choice == "2":
        print("\nREMOVE POINTS FROM AN ATTRIBUTE")
        print("You have", pool, "points to spend.")
        print(
        """
        Choose an attribute:
           strenght
           health
           wisdom
           dexterity
        """
        )
        at_choice = input("Your choice: ")
        if at_choice.lower() in attributes:
            points = int(input("How many points do you want to remove: "))
            if points <= int(attributes[at_choice]):
                pool += points
                result = int(attributes[at_choice]) - points
                attributes[at_choice] = result
                print("\nPoints have been removed.")
            else:
                print("\nThere are not that many points in that attribute")
        else:
            print("\nThat attribute does not exist.")

    elif choice == "3":
        print("\n", attributes)
        print("Pool: ", pool)
    else:
        print(choice, "is not a valid option.")



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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.