AlphaTested
AlphaTested

Reputation: 1057

Programming Python for absolute beginners: Chapter 5 Challenge #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

Upvotes: 2

Views: 4571

Answers (4)

Guest
Guest

Reputation: 1

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.")

Upvotes: 0

Lennart Regebro
Lennart Regebro

Reputation: 172249

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.

Upvotes: 0

SingleNegationElimination
SingleNegationElimination

Reputation: 156168

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.

Upvotes: 0

sinelaw
sinelaw

Reputation: 16553

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

Upvotes: 3

Related Questions