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)
``````
2
Contributors
3
Replies
6
Views
6 Years
Discussion Span
Last Post by pyTony

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'])
``````

Edited by pyTony

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.