Question

I can't seem to know a solution to how my_damage can create a new random value.

That's because while keeps the random.randrange the same.


So let's say:

my_damage = random.randrange(40, 45)

Then we add the combat formula:

jack_health = (jack_health + jack_defense) - my_damage
print(deskemon + " HAVE INFLICTED " + str(my_damage) + " TO CHIPHEAD")

Then this comes out:

LILWILLY HAVE INFLICTED 41 TO CHIPHEAD
CHIPHEAD HAS 261 HITPOINTS LEFT!
...
...
...
CHIPHEAD HAVE INFLICTED 5 TO LILWILLY
LILWILLY HAS 261 HITPOINTS LEFT!
BATTLE MENU:

(1) ATTACK
(2) SPECIAL ATTACK
> 1
LILWILLY HAVE INFLICTED 41 TO CHIPHEAD
CHIPHEAD HAS 222 HITPOINTS LEFT!
...
...
...
CHIPHEAD HAVE INFLICTED 5 TO LILWILLY
LILWILLY HAS 222 HITPOINTS LEFT!

Everytime, I use the code, the random value stays the same, it doesn't change.

I know a solution but because my_damage can be changed via input to 4 other random values earlier in my program, I cannot use this:

jack_health = (jack_health + jack_defense) - random.randrange(20, 25)
print(deskemon + " HAVE INFLICTED " + str(random.randrange(20, 25)) + " TO CHIPHEAD")

Notes:

Here's entire code in pastebin.com if you need information: http://pastebin.com/iij5C16z

Was it helpful?

Solution

When you call a random number, the function return a single value. Hence your error. You need to come up with a way of calling the function every time you want a new value.

There is something clever that you can do that requires decorators that I will show below.

def recall_last(func):
    def _inner(*args, **kwargs):
        if args:
            func.last_dealt = func(*args,**kwargs)
        else:
            func.last_dealt = getattr(func,'last_dealt',0)
        return func.last_dealt
    return _inner

@recall_last
def rand_damage(*args):
    return random.randrange(*args)

This code will return a new value every time you pass it an lower and upper bound to use in the randrange function. If nothing is passed, then it returns the last value generated. Then this would work in your code

if deskemon_selection_input == "1":
    deskemon = "TIMOSHA"
    my_health = 500
    my_damage = (20, 25)
    my_defense = random.randrange(10, 15)
    my_specialattack = 0
    print("You have selected TIMOSHA as your Deskemon")
    deskemon_selection_loop = False

   ....
jack_health = (jack_health + jack_defense) - rand_damage(*my_damage)
print(deskemon + " HAVE INFLICTED " + str(rand_damage()) + " TO CHIPHEAD")

OTHER TIPS

i know this should be implemented with classes but i am still learning classes so this is not using classes:

import random

def damage( attack ):
    return random.randrange(attack*0.9, attack*1.1)

my_name = 'LILWILLY' 
my_health = 300
my_defense = 10
my_attack = 40

enemy_name = 'CHIPHEAD'
enemy_health = 100
enemy_defense = 1
enemy_attack = 40

while my_health > 0 and enemy_health > 0:
    my_damage = damage( my_attack )
    enemy_damage = damage( enemy_attack )
    my_health = (my_health + my_defense) - enemy_damage
    enemy_health = (enemy_health + enemy_defense) - my_damage
    print( '{0} HAVE INFLICTED {1} TO {2}'.format( my_name, my_damage, enemy_name ))
    print( '{0} has {1} hitpoints left'.format(enemy_name, enemy_health ) )
    print( '{0} HAVE INFLICTED {1} TO {2}'.format( enemy_name, enemy_damage, my_name ))
    print( '{0} has {1} hitpoints left'.format(my_name, my_health ) )

else:
    if my_health > 0:
        print ( '***{} has slain {}!'.format(my_name, enemy_name))
    elif enemy_health > 0:
        print ( '***{} has slain {}!'.format(enemy_name, my_name))
    else:
        print('BOTH {} and {} have died'.format(my_name, enemy_name))

the result i got:

LILWILLY HAVE INFLICTED 42 TO CHIPHEAD
CHIPHEAD has 59 hitpoints left
CHIPHEAD HAVE INFLICTED 36 TO LILWILLY
LILWILLY has 274 hitpoints left
LILWILLY HAVE INFLICTED 40 TO CHIPHEAD
CHIPHEAD has 20 hitpoints left
CHIPHEAD HAVE INFLICTED 43 TO LILWILLY
LILWILLY has 241 hitpoints left
LILWILLY HAVE INFLICTED 41 TO CHIPHEAD
CHIPHEAD has -20 hitpoints left
CHIPHEAD HAVE INFLICTED 40 TO LILWILLY
LILWILLY has 211 hitpoints left
***LILWILLY has slain CHIPHEAD!
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top