vendredi 26 juillet 2019

Random number for variable when called

I'm fairly new in programming and python in general. I already learned for, ifs, and while, and im trying to make a pretty decent rpg/dungeon game. and i'm wondering how do i make a variable have a random number when used for the "damage"

i used randint(a, b) for the variable but it is only random once

This is a creature example

name_slime = "Red Slime"
slime_hp = randint(100, 200)
slime_atk = randint(1, 2)
slime_crit = 60
slime_evade = 10
slime_exp = randint(1, 5)
slime_chance = randint(50, 60)

And these will get called on another function

def battle(enemy_name, enemy_hp, enemy_atk, enemy_crit, enemy_evade, enemy_exp):# Battle sequence

    while enemy_hp > 0:
        global player_hp, potion_count

        print(f"{enemy_name}'s Stats:")
        print(f"HP: {enemy_hp}  Power: {enemy_atk}  Crit Chance: {enemy_crit}%  Evade: {enemy_evade}%\n")
        print("Your Stats:")
        ....

        print("Action:\n1. Attack\n2. Parry\n3. Use Potion")
        choice = input("> ")


        if choice == "1":
            ....

        elif choice == "2":
            ....

        elif choice == "3":
            ....


    print(f"You gained {enemy_exp} EXP")
    exp_check(enemy_exp)
    print(f"Your EXP is now {player_exp}/{exp_limit}")

P.S: i scraped a lot pf things in this code because it's quite long for there is a lot of lines for the calculation

Here's the full code if anyone can help: https://pastebin.com/iFMZyY4z

I'll just take the exp for this case. In the variable "slime_exp" it should give a number between 1 and 5. But when i tried fighting the creature multiple times in one run (not exiting the terminal) it always give the same amount of exp each time. I'm thinking that the randint(1, 5) is defined when the script is run and not when the variable is used. How can i make it so that it will be random when the variable is used?




Aucun commentaire:

Enregistrer un commentaire