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

    Join Date
    Sep 2013
    Posts
    2
    Rep Power
    0

    Python 2 using range statement to change strings


    I'm looking for a statement that can make a variable change based on user input;
    I've created a long winded work around but want to learn if there is an easier way.

    Problem: I created a basic damage output reading based on a character's level.
    Every other level the character levels an ability which increases its base damage output by 20 for 9 levels, and after that the base damage output remains static.

    This is what I've done:
    Code:
    if Mystic_Shot in range (1, 3):
    	x = 35 + total_ad + (TF_ap * .2) + TF_proc
    	print "Mystic Shot by itself will do %s damage" % x;
    if Mystic_Shot in range (3, 5):
    	x = 55 + total_ad + (TF_ap * .2) + TF_proc
    	print "Mystic Shot by itself will do %s damage" % x;
    if Mystic_Shot in range (5, 7):
    	x = 75 + total_ad + (TF_ap * .2) + TF_proc
    	print "Mystic Shot by itself will do %s damage" % x;
    if Mystic_Shot in range (7, 9):
    	x = 95 + total_ad + (TF_ap * .2) + TF_proc
    	print "Mystic Shot by itself will do %s damage" % x;
    if Mystic_Shot in range (9, 19):
    	x = 115 + total_ad + (TF_ap * .2) + TF_proc
    	print "Mystic Shot by itself will do %s damage" % x;
    What i wonder is if there is a way to make this much shorter.

    I googled and saw the map function but didn't totally understand it.

    Maybe something like:

    Code:
    if level in range(1, 9, 2)
    	Mystic_Shot = range_numerals * 20 + 35
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    479
    Rep Power
    33
    Generally you use a list of lists, or tuples. This also makes it easy to change, add, and/or delete parameters. Note that the Python Style Guide uses lower case and underlines for variable names (and 4 spaces, not tabs). CamelCase is for class names. This helps others understand your code, so RunMystic() is a class instance and run_mystic() is calling a function.
    Code:
    def add_to_it(mystic_shot):
        ## add print statements if you don't understand this
        for upper, total in ((3, 35), (5, 55), (7, 75), (9, 95), (19, 115)):
            if mystic_shot < upper:
                return total
    
    for num in (2, 3, 7, 10, 18):
        print num, "-->",
        print add_to_it(num)
    If you want to get fancy you can come up with some kind of formula (although this doesn't work for the final parameter 9->19).
    Code:
    def add_to_it2(mystic_shot):
        total = 35
        total += 20 * ((mystic_shot-1)/2)
        return total
    
    for num in (2, 3, 7):
        print add_to_it2(num)

    Comments on this post

    • vortko agrees
    Last edited by dwblas; September 14th, 2013 at 02:46 PM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    2
    Rep Power
    0
    Thanks for the info dwblas! It functions the same but is around 40 lines shorter (after doing it to several other abilities.)
    I bookmarked the style guide link as well.

IMN logo majestic logo threadwatch logo seochat tools logo