I'm trying to make a battle function but am having some trouble. Is there a way to do this? At the moment I'm getting an error "AttributeError: 'function'object has no attribute 'att'. My guess is that this would be easier with classes, but I've not learned classes yet. So is this possible?

import random

def player():
    hp = 100
    att = random.randint(10, 30)

def rat():
    hp = 20
    att = random.randint(5, 25)

def attack(player, enemy):
    attack = player.att - enemy.hp
    print(enemy.hp)

Use dictionary of strengths. Your functions player and rat do nothing.

import random

player= dict(
    hp = 100,
    att = random.randint(10, 30))

enemy = dict(
    hp = 20,
    att = random.randint(5, 25)
)

attack = player['att'] - enemy['hp']

print(enemy['hp'])

Thanks pyTony, I've been trying to expand a little but I'm getting problems with everything I've tried. Here's my latest attempt.

import random
running = True

player = dict(
    hp = 100,
    att = random.randint(10,30))

enemy = dict(
    name = "Rat",
    hp = 20,
    att = random.randint(5, 25))

def attack(player, enemy):
    global running
    firstAtt = random.randint(1, 2)#1 player goes first, 2 enemy goes first
    while running:
        if firstAtt == 1:
            if enemy["hp"] > 0:
                playerAttack = player["att"]
                enemy["hp"] = enemy["hp"] - playerAttack
                print("You have dealt {0} damage to the {1}!".format(playerAttack, enemy["name"]))
            elif player["hp"] > 0:
                enemyAttack = enemy["att"]
                player["hp"] = player["hp"] - enemyAttack
                print("The {0} has dealt {1} damage to you!".format(enemy["name"], enemyAttack))
            else:
                running = False

This one crashes my editor, I think it's because the while loop isn't exiting out. Any ideas/tips?

You are not covering case when firstAtt is 2

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.