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.