# [Tutor] newbie needs help with rpg code in python

Marc Tompkins marc.tompkins at gmail.com
Sun Mar 16 20:57:37 CET 2008

```On Sun, Mar 16, 2008 at 10:59 AM, jake cooper <lord_cooper at hotmail.co.uk>
wrote:

>     print 'You inflicted', damage_to_enemy, "points of damage to your
> enemy"  #shows damage dealt
>     temp_e_hp - damage_to_enemy #!!!attempts to deduct damage_to_enemy
> from temp_e_hp!!!
>     print temp_e_hp**
>
*    if temp_e_hp == 0:***
>
*         victory()***
>
For some reason, I cant manage to subtract damage_to_enemy from temp_e_hp.
> Any help with my script (especially with this problem) would be highly
> appreciated.
>

You're subtracting, but not assigning the result of your subtraction to
anything.  Try this:
temp_e_hp =  temp_e_hp - damage_to_enemy

May I suggest that you change your victory condition from "equals 0" to
"less than or equal to 0"?  As it stands, the player can never win unless he
hits for exactly as much damage as the enemy has remaining - if he goes
over, the enemy becomes immortal.

And for one final question, is there a way to use random percentages of a
> number.  For example, to subtract 80 - 120 % of damage_to_enemy from
> temp_e_hp?
>

The random module is what you want.
random.random() returns a number between 0 and 1 - you can use that
directly for percentage calculations.
OR
random.randint(a,b) returns an integer between a and b inclusive - e.g.,
if a=1 and b=10, possible results include 1 and 10.

Something like this, perhaps:
import random   # this goes at the top of your code
temp_e_hp =  temp_e_hp - random.randint( (damage_to_enemy*0.8),
(damage_to_enemy*1.2))

Of course, you shouldn't hard-code values like 0.8 and 1.2 - but I wanted to
keep it simple.

--
www.fsrtechnologies.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20080316/7df352d0/attachment-0001.htm
```