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

    Join Date
    Apr 2013
    Posts
    6
    Rep Power
    0

    Advice needed for n00b


    Hi, i've been learning python from the book python programing for the absolute beginner, which includes challenges at the end of each chapter. I've been doing one which involves spending skill points for an rpg char (and reassigning points). The problem i'm having is, using two different while loops, when it runs to the end of the second it wont run throughthe first loop again, even when the conditions are false for the first loop. heres what i have:

    #allow skill point spending for character

    skill = {'strength':0 , 'health':0 , 'wisdom':0 , 'dexterity':0 }
    points = 30

    print 'you have' ,points, 'points to spend.'
    print '''
    you can spend them in
    strength
    health
    wisdom
    dexterity
    '''

    while points:
    y = raw_input('which skill to spend points in:')
    if y in skill:
    x = int(raw_input('how many points to assign:'))
    skill[y] = x
    print 'you have put',x,'points into the',y,'skill.'
    else:
    print 'this is not valid'

    points -= (x)

    while points <= 0:
    reas = raw_input('would you like to reassign any points? y/n')
    if reas == 'y':
    x = raw_input('which skill to remove points from?')
    if x in skill:
    z = int(raw_input('how many points to remove?'))
    skill[x] -= z
    print 'you have removed' ,z, 'from the' ,x ,'skill'
    else:
    print'not valid'
    else:
    print 'thanks'

    points += (z)

    raw_input('\n\nenter to exit')

    Please excuse my lack of comments, i'm sure theres an easier way to do it but that will probably be covered in a later chapter. I'd just like to make sure i know how to use these loops. Specifically how to get it to check against the first loop. Any help would be appreciated, not really after a code to copy just some hints would be helpful, thanks very much in advance, matt.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    191
    Rep Power
    2
    Firstly, it is essential that you use code tags in programming forums. Even more so for python since whitespace is actually a part of the syntax.

    Basically you need to have while loops within while loops. Programs like this end up looking incredibly ugly because you have not yet learned functions. Functions would allow us to write this same program in a much saner way.

    Anyway, here is an example implementation without functions. I think it is glitch free but no guarantees :
    python Code:
    skill_minimums = {'strength':0 , 'health':0 , 'wisdom':0 , 'dexterity':0 }
    skill = skill_minimums.copy()
    points = 30
     
    print ('you have {} points to spend.\n'.format(points))
    print ("You can spend them in: ")
    for atr in skill:
        print(atr.capitalize())
     
    done = False
    while not done:
        stat = None
        while stat not in skill:
            if stat != None:
                print("Invalid choice.")
            stat = raw_input('Which skill would you like to modify? ').lower()
     
        up_or_down = None
        while up_or_down not in ["inc","dec"]:
            if up_or_down != None:
                print("Invalid choice.")
            up_or_down = raw_input("Would you like to increase or decrease {} (inc,dec)? ".format(stat))
     
        while 1:
            try:
                amount = int(raw_input("How many points would you like to {}rease {} by? ".format(up_or_down,stat)))
                if amount < 1:
                    raise ValueError
                break
            except ValueError:
                print("Invalid choice.")
     
        if up_or_down == "inc":
            if points >= amount:
                skill[stat] += amount
                points -= amount
            else:
                print("You don't have enough free points.")
        elif up_or_down == "dec":
            if skill[stat]-amount >= skill_minimums[stat]:
                skill[stat] -= amount
                points += amount
            else:
                print("\nYou can't decrease {} below {}".format(stat,skill_minimums[stat]))
     
        print("")
        for attr in skill:
            print("{:10}: {}".format(attr.capitalize(),skill[attr]))
     
        if points > 0:
            print("\nYou have {} points left to allocate.\n".format(points))
        else:
            print("\nYou have no free points remaining.")
            print("Would you like to continue editing?")
            if raw_input("Enter 'y' to continue editing: ") != "y":
                done = True
            print("")
     
    print("Enjoy your adventure.")


    -Mek
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    6
    Rep Power
    0
    Thanks for the response, i didn't preview post, sorry. stupid question but how do i insert code properly on here?

    I guessed that the next chapter in my book (about functions)would tidy things up a lot for me, just wanted to make sure i had a grasp of loops before i moved on. Is the script you posted in python 3? Again thanks for taking the time to post, think i only need to tweak it, mostly works just not quite how i wanted it to, i had suspected that i would need to move the second loop inside the other so guess ill plod on and try to figure it out!!! thanks again, matt
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    191
    Rep Power
    2
    There is a highlight button, and a code button, in the tool bar when you post. You should be able to figure it out from there.

    My code is python 2.x (it uses raw_input). I always put parenthesis around my print statements so that they can run in py2 and 3. I could have made this py3 compatible but I didn't bother.

    Did you have trouble running it?

    -Mek
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    6
    Rep Power
    0
    cool cheers, was just curious really havent tried runing it yet was kinda wanting to get working what ive done, like to learn from my mistakes

IMN logo majestic logo threadwatch logo seochat tools logo