Thread: Code critique?!

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    29
    Rep Power
    0

    Code critique?!


    Hey! I just wrote a rudimentary character creator program and the code ended up being extremely long, like 350 lines.

    Anyhow I was just curious if I could get some critique on it but I don't want to post it because its so long, but I can't figure out how to attach it as a zip. Do I need to host it myself or does this forum have an attachment option?
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    12
    Rep Power
    0
    You could try using Pastebin and adjust the privacy settings as you wish.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    29
    Rep Power
    0
    Hey thanks StevenJM. I'll do is ASAP!!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    29
    Rep Power
    0
    Character Creator!

    The only issue I couldn't resolve here is with this bit of code:

    Code:
                while allo_strength > stat_pool_length or allo_strength < 1:
                    print("\nInvalid choice.")
                    print("You have",stat_pool_length,"stat points in your Stat Pool.")
                    allo_strength = int(input("\n(SUB)Please choose a valid amount to allocate: "))
                    if allo_strength > stat_pool_length or allo_strength < 1:
                        continue
                    else:
                        break
    Basically I can't figure out how to check the input for letters or an accidental enter hit. Right now it's only protected from integer inputs. If there is an accidental letter or the enter key is hit an error happens and it exits the program.

    I am sure there is a lot wrong with this program and I have a feeling something like this could be done with WAY less lines of code.

    Is my first significant program from scratch so I am proud of it!! But if your generous enough to take the time to look at it please be as critcal as you feel necessary! I must learn how to code, I love it!!

    Any feedback is greatly appreciated!!!

    P.S. Also thanks b49P23TIvg for showing me the .join() function.

    EDIT: Thanks StevenJM for leading me to Pastebin, awesome website.
    Last edited by jomiscli; January 3rd, 2013 at 05:57 PM. Reason: forgot to thank StevenJM for leading me to Pastebin, what an awesome website!
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,702
    Rep Power
    480
    We abhor duplicate code. This must have been a nightmare to write. "Gotta make the change in 4 places. OH NO!"

    You have 4 cases all the same except for the attribute name and which lists are used. Encapsulate that into a function.

    You have many prompts with validity tests. Again, encapsulate into a function.

    I started rewriting as
    Code:
    stat_pool = ['|']*30
    strength = []
    health = []
    wisdom = []
    dexterity = []
    
    choice = None
    
    def prompt(message,validSet,tries,failureChoice):
        M = message
        result = failureChoice
        for i in range(tries):
            result = input(M)
            if result in validSet:
                return result
            M = 'Please choose from %s'%str(validSet)
        return failureChoice
    
    while choice != "6":
        attribute = prompt('(MAIN)Please choose an option: ',set('123456'),2,'6')
    
        if attribute in set('1234'):
            attribute_name = {
                '1':'Strength','2':'Health','3':'Wisdom','4':'Dexterity'
                }[attribute]
    You may need a somewhat more clever prompt function with a validation function and exception handling when you get to the numbers representing amounts of stat points to transfer.
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    29
    Rep Power
    0
    Thank you for the response. Going to learn a ton from this again!!

IMN logo majestic logo threadwatch logo seochat tools logo