0

Newbie here. I'm making a simple fighting text game. I'm trying to generate a random enemy, then use that result throughout a fight, but my code keeps randomizing an enemy every time I call enemy. Sorry, it's hard for me to explain, but here is my code...

import random

loot = ["Laboratory Key"]
enemies = ["The Scientist", ["Max Health", 10], ["Health", 10], ["Base Attack", 2], ["Base Defense", 2], ["Money Gain", 10], ["Loot Drop", loot[random.randint(0, 0)]]], ["Human", ["Max Health", 50], ["Health", 50], ["Base Attack", 10], ["Base Defense", 10], ["Money Gain", 25], ["Loot Drop", loot[random.randint(0, 0)]]], ["Little Bob", ["Max Health", 100], ["Health", 100], ["Base Attack", 20], ["Base Defense", 20], ["Money Gain", 50], ["Loot Drop", loot[random.randint(0, 0)]]]
player = ["Space Robot Kyle", ["Max Health", 100], ["Health", 100], ["Base Attack", 10], ["Base Defense", 10], ["Dollars", 0]]
inventory = []


def fight():
    global enemy
    enemy = enemies[random.randint(0, 2)]

    print(player[0], "V.S.", enemy)
    print("Your stats: ")
    for stats in player:
        print(stats)
    print('  ')
    print("Enemy Stats: ")
    for stats in enemy:
        print(stats)
    print('  ')
    print('1.) Attack')
    print('2.) Drink oil can')
    print('3.) Inventory')
    print('4.) Run')
    option = input('--> ')
    print('  ')
    if option == '1':
        attack()
    if option == '2':
        drinkOil()
    if option == '3':
        print(inventory)
    if option == '4':
        run()
    else:
        fight()


def attack():
    enemyAttackChance = random.randint(0, 20)
    playerAttackChance = random.randint(0, 20)
    if playerAttackChance < 7:
        print("You missed!")
    else:
        print("A direct hit!")
        input("--> ")
        print("You deal ", player[3][1], "damage.")
        enemy[2][1] -= player[3][1]
    input('--> ')
    print('  ')
    if enemy[2][1] <= 0:
        enemy[2][1] = enemy[1][1]
        win()
    else:
        if enemyAttackChance < 7:
            print('  ')
            print('The enemy missed!')
            print('  ')
        else:
            print('Oh no! You\'ve been hit!')
            print('You take ', enemy[3][1], 'damage')
            print('  ')
            player[2][1] -= enemy[3][1]
    if player[2][1] <= 0:
        dead()
    input('--> ')
    
def win():
    print("You defeated", enemy[0], "!")
    print("  ")
    input("--> ")
    print("You got paid", enemy[5][1], "dollars!")
    player[5][1] += enemy[5][1]
    print("  ")
    input("-->" )
    print("It also dropped a", enemy[6][1])
    inventory.append(enemy[6][1])
    input("--> ")
    lobby()       


fight()

That's what I tried and I was expecting it to choose a random enemy then store that random enemy in the variable enemy.

  • It is doing what you are expecting but it is doing it every time. You need to check if an enemy was already set before attempting to set it to random one. Either use another variable, or check if the value is `None`. But first, you'll have to initialize `enemy` at the global scope and set its value to `None`. – Random Davis Feb 14 '23 at 17:33

0 Answers0