Let me start out by saying I've been playing with Python for a few weeks now, was recommended by a friend who uses Perl, and it's been quite enjoyable to play with. I've no other programming experience except HTML/CSS. This site has been a wealth of information and an excellent tool in my quest for knowledge.

I've been baffled for awhile on this now and I've searched all the way to page 63 to see if this has been asked before, needless to say, I couldn't find a clear answer to my problem.

To clear things up a bit, I had a problem with random only running once within the program so I had to added the 'global' so it would run every time the program was run. Something tells me this made things more complicated than it needed to be.

Either way, my issue is that I can not figure out a way to make my 'hp' count down as I continue to 'attack'. I add the 'hp = 100' within the 'def' for attacking and it always tells me that it is not defined when clearly it is. So that led me to add it outside of the 'def' for the attack, but then that leads the program to keep resetting the 'hp' back to 100 every time it is run. This makes for some long battles obviously.

The following program does not include any attempts at the 'hp' ticking down, just a working attack system.

import random, textwrap

cho = '\t1.Attack'

att = 'You attack!'

hp = 100

def ran_ran():
    global ran
    ran = random.randrange(10)

def crt_ran():
    global cran
    cran = random.randrange(10)

def dth_ran():
    global deth
    deth = random.randrange(10)

def bat_sta():
    print cho
    des = int(raw_input(' \tPick! '))
    if des == 1:
        des = att
        print textwrap.fill(des)
        ran_ran()
        crt_ran()
        dth_ran()
        if (ran == cran and
            cran == deth):
            print 'You killed it!'
            bat_sta()              #Added to make programming checking easy.
        if (ran == cran and
            ran > 0):
            print 'Critical hit! %i points of damage!' % (cran * 2)
            print 'Enemies hp: %i' % (hp - (ran * 2))
            bat_sta()
        if ran == 1:
            print 'Your attack does 1 point of damage!'
            print 'Enemies hp: %i' % (hp - ran)
            bat_sta()
        if ran > 1:
            print 'Your attack does %s points of damage!' % ran
            print 'Enemies hp: %i' % (hp - ran)
            bat_sta()
        if ran == 0:
            print 'Your attack didn\'t do anything!'
            bat_sta()
    else:
        bat_sta()

bat_sta()

Any help would be awesome and I thank you in advance.

You should be using return values instead of global variables... see if this makes any sense, and if not ask questions.

>>> import random
>>> def get_ran():
...     return random.randint(1,10)
...     
>>> get_ran()
5
>>> get_ran()
10
>>> get_ran()
4
>>> hp = 100
>>> hp -= get_ran()
>>> hp
97
>>> hp -= get_ran()
>>> hp
87
>>> hp += get_ran()
>>> hp
95
>>>

It makes perfect sense, but when used within the context in which I'm using it I still run into the same problem. I cannot get 'hp' into a position that sets it as '100' without resetting it as '100' every time. It works fine until I have to loop back to where the user has a choice to attack again or do something else.

The 'global' was added because otherwise it would run 'ran = random.randrange(10)' and give me a number, but no matter how many times I ran it after, it would continue to give me the same number.

I feel using get_ran(), like you showed, isn't as practical for my purposes either. While running my code using your examples I don't get the calculations I expect, despite not changing the format of the program much. After all I'm not just using get_ran() in most cases, I also have it calculating against other random numbers.

Obviously this is all just ignorance of the language on my part. It appears to be time to hit the books again. Thank you for your assistance.

Here, I don't think you understood what I was saying. I updated your code to slightly reflect the way that I would write it...

import random, textwrap

cho = '\t1.Attack'
att = 'You attack!'

def get_ran():
    random.seed()
    return random.randrange(10)

def bat_sta( hp ):
    print cho
    des = int(raw_input(' \tPick! '))
    if des == 1:
        ran = get_ran()
        cran = get_ran()
        deth = get_ran()
        #print 'Ran:',ran,'Cran:',cran,'Deth:',deth
        print textwrap.fill(att)
        if (ran == cran and ran > 0):
            print 'Critical hit! %i points of damage!' % (cran * 2)
            hp -= ran * 2
        elif ran == 1:
            print 'Your attack does 1 point of damage!'
            hp -= ran
        elif ran > 1:
            print 'Your attack does %s points of damage!' % ran
            hp -= ran
        elif ran == 0:
            print 'Your attack didn\'t do anything!'
        print 'Enemies hp: %i' % hp
        if hp == 0:#(ran == cran and cran == deth):
            print 'You killed it!'
    
    return hp


if __name__ == '__main__':
    hp = 100
    while True:
        hp = bat_sta(hp)

Please ask about anything you don't understand

From the looks of it, it appears that I didn't understand what you meant. Exactly what I was looking for, you are awesome.

I've still a lot to learn and I look forward to learning it, thank you for assisting me in my endeavors.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.