I'm trying to use a new enemy every time the hero levels up. The code itself works without a problem. The player levels, up, he uses magic and mana, the enemy hits back, and all that jazz. I just can't figure out a way to throw in a new enemy every time the player kills the enemy. Any ideas?
class Character:
def __init__(self, name, health, level):
self.name = name
self.health = health
self.level = level
self.levelup_counter = level + 1
def attack(self):
c2.health -= 2
c1 = Character("Hero", 10, 1)
c2 = Character("Monster 1", 10, 2)
c3 = Character("Monster 2", 20, 3)
c4 = Character("Monster 3", 40, 4)
# monster_list = [c2,c3,c4]
def stats():
print("_______________________________________")
print("Your stats \t\tEnemy Stats")
print("---------------------------------------")
print(f"Health: {c1.health}\t\tHealth: {c2.health}")
print(f"Level: {c1.level}\t\tLevel: {c2.level}")
while True:
attack = input("Enter 1 to attack: ")
c1.attack()
stats()
if c2.health == 0:
c1.level += 1
print("YOu killed the monster!\nYou leveled up!")
continue
UPDATE:
UPDATE:
I made a few changes and added a for loop to iterate over the monster list. Now I have two problems. First, the monsters keep iterating (obviously) instead of simply one monster fighting until death. Then, a new monster will appear. Second, I can't remove a monster from the list after death. I get the error "TypeError: 'Character' object cannot be interpreted as an integer"
class Character:
def __init__(self, name, health, level):
self.name = name
self.health = health
self.level = level
self.levelup_counter = level + 1
def attack(self):
m.health -= 2
def monster_death(self):
if m.health == 0:
monster_list.pop(m)
c1 = Character("Hero", 10, 1)
c2 = Character("Monster 1", 10, 2)
c3 = Character("Monster 2", 20, 3)
c4 = Character("Monster 3", 40, 4)
monster_list = [c2,c3,c4]
def stats():
print("_______________________________________")
print("Your stats \t\tEnemy Stats")
print("---------------------------------------")
print(f"Health: {c1.health}\t\tHealth: {m.health}")
print(f"Level: {c1.level}\t\tLevel: {m.level}")
i=0
while True:
i+=1
for m in monster_list:
attack = input("Enter 1 to attack: ")
c1.attack()
stats()
if m.health == 0:
c1.level += 1
print("YOu killed the monster!\nYou leveled up!")
m.monster_death()
continue
To fix the first issue, just add a while loop for each monster that continues until the health is less than or equal to 0. I have shown this in the code below. Also your "i" counter was completely unused from what I could tell.
while True:
for m in monster_list:
while m.health > 0:
attack = input("Enter 1 to attack: ")
c1.attack()
stats()
if m.health <= 0:
c1.level += 1
print("YOu killed the monster!\nYou leveled up!")
m.monster_death()
As for the second issue with removing the monsters from the list, I think that is a bad idea while you are iterating through a for loop. By removing the monsters from the list while looping through them, you are going to get indexing errors and it will skip monster number 3. If you want to get rid of the error you are getting though, simply change this line: monster_list.pop(m) to monster_list.remove(m).
What #furas said above is a good suggestion. You could do something like this (pseudocode but you get the idea):
while len(monster_list) > 0:
while monster_list[0].health > 0:
# attack
if monster_list[0].health <= 0:
# kill monster and remove monster_list[0]
You could keep monsters on list and fight always with first monster on list - monster_list[0]. And when you kill it then remove() it - and then you can append new monster at the end of list.
Minimal working code
# --- classes ---
class Character:
def __init__(self, name, health, level):
self.name = name
self.health = health
self.level = level
self.levelup_counter = level + 1
def attack(self):
self.health -= 2
def monster_death(self):
if self.health <= 0: # better check `<=` instead of `==`
monsters.remove(self)
def __str__(self):
return f'name: {self.name}, health: {self.health}, level: {self.level}'
# --- functions ---
def stats(player, monster):
print("_______________________________________")
print("Your stats \t\tEnemy Stats")
print("---------------------------------------")
print(f"Health: {player.health}\t\tHealth: {monster.health}")
print(f"Level: {player.level}\t\tLevel: {monster.level}")
# --- main ---
player = Character("Hero", 10, 1)
monsters = [
Character("Monster 1", 10, 2),
Character("Monster 2", 20, 3),
Character("Monster 3", 40, 4),
]
#monsters = []
#for i in range(1, 4):
# monsters.append( Character(f"Monster {i}", i*10, i+1),
i = 3 # used for monster's name
while monsters:
m = monsters[0]
print('New monster attacked:', m) # it will use `__str__` to display `m`
while True:
attack = input("Enter 1 to attack: ")
m.attack()
stats(player, m)
if m.health <= 0: # better check `<=` instead of `==`
player.level += 1
print("You killed the monster!\nYou leveled up!")
m.monster_death()
i += 1
monsters.append( Character(f"Monster {i}", i*10, i+1) )
break # exit nearest `while`
Related
I'm trying to create a blank dictionary that we add user inputs to (Character Name, Age, HP). Later on I will use those inputs to create new classes instances.
Here's the result as well as a note about what I was expecting to see:
I believe the problem lies in this part of the code but I'm not sure what's wrong with it.
newCharacterList = {"Name": [], "Age" : [], "HP" : []}
newCharacterList["Name"].append(newCharacterName)
newCharacterList["Age"].append(newCharacterAge2)
newCharacterList["HP"].append(newCharacterHP)
print(newCharacterList)
Full code:
import random
# Parent class of Character (Name, Age, HP) - keep it simple - no atk and defense. just keep losing HP based on a random number between 1 and 50 and once it reaches 0 it says "Whoops u died lol." but it prints out on a different row each time, dmg taken and HP remaining (maybe the random number lasts 5 x and each character may survive or die each round depending on how much hp they have.)
class Character:
def __init__(self, name, age: int, HP):
self.name = name
self.age = age
self.HP = HP
def get_name(self):
return self.name
def get_age(self):
return self.name
def get_HP(self):
return self.HP
def set_HP(self):
subtraction = int(random.randint(1,50))
print("HP Loss: -",subtraction)
self.HP = self.HP - subtraction
def ShowAttributes(self):
print("Name: ", self.name)
print("Age: ", self.age)
print("HP: ", self.HP)
Melo = Character("Melo", 15, 100)
Melo.ShowAttributes()
print("\n")
Halo = Character("Halo", 20, 500)
Halo.ShowAttributes()
print("\n")
Sky = Character("Sky", 18, 300)
Sky.ShowAttributes()
print("\n")
# add option to ADD a character which will take the Name, Age, and Randomly assign an HP.
# it will then note "HP Assigned. Name has _ HP."
# Press X to Start the game, A to Add a character, or any other key to Exit.
while True:
# print(ShowAttributes())
print("Press X to Start, A to Add a character, or any other Key to Exit\n");
button = input("Your Input: ")
# #round 1 (- random number of HP once)
# #round 2 (- random number of HP TWICE)
# #round 3 (- random number of HP Three times)
# #round 4 (- random number of HP 4 times)
# #round 5 (- random number of HP 5 times)
# # when ALL character's HP = 0 OR all 5 rounds are finished, whichever comes first, print the order of winners (ie who survived the longest), then break and exit.
if button == "x":
# While LOOP (until all rounds are done OR all characters run out of HP) {
print("\n")
# }
while Melo.get_HP() > 0:
# print("its working"); #< Test Completed!! - it works!
# While LOOP (until all rounds are done OR all characters run out of HP)
print("Current HP: ", Melo.get_HP())
Melo.set_HP()
if Melo.get_HP() <=0:
print(f"Whoops, {Melo.name} has No More HP!")
break
elif button == "a":
class add_Character(Character):
def __init__(self):
super().__init__()
self.HP = HP
print("In Development - for Phase 2");
print("New Character Name: ")
newCharacterName = input()
print("New Character Age: ")
newCharacterAge = input()
newCharacterAge2 = int(newCharacterAge)
if newCharacterAge2 < 10:
newCharacterHP = (random.randint(1,50))
elif newCharacterAge2 > 10 and newCharacterAge2 < 25:
newCharacterHP = (random.randint(50, 300))
elif newCharacterAge2 < 25:
newCharacterHP = (random.randint(300, 1000))
else:
newCharacterHP = (random.randint(1,1000))
print("Your New Character's Autogenerated HP is: ", newCharacterHP)
newCharacterList = {"Name": [], "Age" : [], "HP" : []}
newCharacterList["Name"].append(newCharacterName)
newCharacterList["Age"].append(newCharacterAge2)
newCharacterList["HP"].append(newCharacterHP)
print(newCharacterList)
else:
break
else:
exit
Move newCharacterList = {"Name": [], "Age" : [], "HP" : []} before your while statement.
A few comments to improve your code:
if Melo.get_HP() <=0: should be removed (useless considering the while above).
random.randint(1,50) is already an int, you don't have to write int(random.randint(1,50))
Your get_age method actually returns the name. By the way, is there a particular reason to use these getters?
Your condition elif newCharacterAge2 < 25: is possible only when the age is 10, so it is a weird way to test newCharacterAge2 == 10.
Instead of ShowAttributes, use __str__ as below (then, just try to print(Melo) to see the magic happen):
def __str__(self):
return f"Name: {self.name}\nAge: {self.age}\nHP: {self.HP}"
I am trying to make it so that the user can input the name of another pokemon class so that I can use the attack function. Is there any way I could do this from user input? It seems as though even when I put in a valid name it returns an error. I would love to know as to how I can do this so that I can finish my game.
import random
class Pokemon(object):
def __init__(self, name, level, pokemon_type, max_health, current_health, is_knocked_out):
self.name = name
self.level = level
self.pokemon_type = pokemon_type
self.current_health = current_health
self.max_health = max_health
self.is_knocked_out = is_knocked_out
#knock out function
def knock_out(self):
self.is_knocked_out = True
print(self.name + " is now knocked out")
#lose health func, takes in one parameter which is damage
def lose_health(self, amount):
if amount > self.current_health:
print(self.name + " has been knocked out and now has 0 hp")
self.current_health = 0
self.is_knocked_out = True
else:
self.current_health -= amount
print(self.name + " now has " + str(self.current_health) + " HP")
#hp regen function, takes in one parameter which is the amount of hp to be regained, wont allow you to go above the max
def regain_health(self, amount):
if amount > self.max_health:
print("You cannot heal past your max HP")
else:
self.current_health += amount
print(self.current_health)
#function to revive dead mons
def revive(self):
if self.is_knocked_out == True:
self.is_knocked_out = False
print(self.name + " has been revived and half of his hp has been restored!")
self.current_health = self.max_health / 2
else:
print("This pokemon is still alive ")
return
#attack function, in progress
def attack(self, pokemon_name, move_number):
if self.current_health > 0 and self.is_knocked_out == False:
if move_number == 1:
pokemon_name.lose_health(random.randint(1,150)) #Here is where the issue is, how can I make user input determine the name of the pokemon which is being attacked and sequentially make it lose HP?
print(pokemon_name + " was attacked")
elif move_number == 2:
pass
elif move_number == 3:
pass
elif move_number == 4:
pass
else:
print("You are knocked out!")
Lucario = Pokemon("Lucario", 12, "fighting", 200, 200, False)
Blastoise = Pokemon("Blastoise", 12, "water", 200,200, False)
Blastoise.attack("Lucario", 1)
just use:
if move_number == 1:
globals()[pokemon_name]
what is globals()? it's a built-in function that load all global variable (predefined or user-defined), it's a dictionary
data = 5
globals()["data"]
every variable you defined, it will be added to globals() as
globals()[name] = value
def attack(self, pokemon, move_number):
if self.current_health > 0 and self.is_knocked_out == False:
if move_number == 1:
pokemon.lose_health(random.randint(1,150)) #Here is where the issue is, how can I make user input determine the name of the pokemon which is being attacked and sequentially make it lose HP?
print(pokemon.name + " was attacked")
elif move_number == 2:
pass
elif move_number == 3:
pass
elif move_number == 4:
pass
else:
print("You are knocked out!")
Characters = dict()
Characters["Lucario"] = Pokemon("Lucario", 12, "fighting", 200, 200, False)
Characters["Blastoise"] = Pokemon("Blastoise", 12, "water", 200,200, False)
Characters["Blastoise"].attack(Characters["Lucario"], 1)
you also can use dictionary. what is dictionary? a dictionary is a collection of named variable, like list, but you can add to it by name...
how to define dictionary?
Characters = dict()
and add your variables:
Characters["<name>"] = value
like:
Characters["Blastoise"] = Pokemon("Blastoise", 12, "water", 200,200, False)
and use variables:
Characters["<name>"].<your-method>
like:
Characters["Blastoise"].attack(Characters["Lucario"], 1)
just one point: if you use dictionary, so you send variable (Pokemon object, not it's name), so attack's second parameter is pokemon and use:
print(pokemon.name + " was attacked")
This question already has answers here:
How to sort a list of objects based on an attribute of the objects in descending order?
(9 answers)
Closed 2 years ago.
Yes, I have looked at other posts but I am still a bit confused, some use lambda or make multiple methods and I am confused.
I have this class, and I want to create multiple instances (members of the team) and before I call my function to start a fight, I want to arrange a list so that the person with the highest self.full_speed goes first, etc. (I have a full_speed and speed attributes for debuffs/buffs)
class Player:
"""Describes the main player."""
def __init__(self, level, health, will, speed):
"""Initializes stats"""
self.level = level
self.health = health
self.full_health = health
self.will = will
self.full_will = will
self._cheat = "cheat" # Protected instance attribute has a leading underscore
# Private starts with two leading underscores
self.speed = speed
self.full_speed = speed
def level_up(self, skill):
"""Choose where to distribute skill points."""
pass
def default_attack(self, enemy):
"""Normal attack that damages enemy target."""
damage = 0
if random.randint(1,100) <= 95:
damage = (self.level * 2)
critical_hit = random.randint(1,10)
if critical_hit == 1:
damage += int(self.level * 0.5)
print("Critical hit!")
enemy.health -= damage
print("The enemy took " + str(damage) + " damage.")
def special_attack(self, enemy):
"""Deals more damage but uses will, more likely to miss."""
damage = 0
if random.randint(1,100) <= 90:
damage = (self.level * 3)
critical_hit = random.randint(1, 10)
if critical_hit == 1:
damage += self.level
print("Critical hit!")
enemy.health -= damage
self.will -= 2
print("The enemy took " + str(damage) + " damage.")
def heal(self):
"""Heals self by 10%."""
self.will -= 2
recovered = int(self.full_health * 0.10)
self.health += recovered
if self.health > self.full_health:
self.health = self.full_health
print("Recovered " + str(recovered) + " HP.")
I've started here but I am unsure where to go at this point..
def team_by_speed(team):
new_team = []
for member in team:
# member.full_speed
return new_team
Used Sorted() function
Call sorted(iterable, key: NoneType=None) with a list of objects as iterable
The title is terrible, but hopefully I can explain in my post. Creating a little game as my pet project for python, and I'm currently creating the inventory. Everything was... ok when developing the game until it came to making the function that will show all of the player's inventory.
elif (prompt == "examine"):
print(inventory[1].name)
gameprompt()
Ok, so I created a list that basically has a bunch of classes from Items in it. To call on the name element of these classes I have to do something like this, otherwise I just get its memory location which is largely useless to the player. I've tried
elif (prompt == "examine"):
print(inventory[].name)
gameprompt()
Thought that this above example would print only the name of all the Item objects, but there's a compilation error instead because I didn't specify which one. So I then tried~
elif (prompt == "examine"):
print(inventory[1:1000].name)
gameprompt()
Thinking that it would print all of the Item objects names up to 1000, but I obviously don't have that so I thought it would print the names up to the latest object that was there and stop but there was another compilation error from this...
If there is anyway to print out an element of a class for all class objects in a list please let me know. The full code of this game is here, although I don't think you'll need it to help me solve my problem (it is also very large.)
playername = input("What is your name?")
zone = 1
movement = 0
restcounter = 0
searchcounter = 0
class Player:
def __init__(self, name, hp, mp, atk, xp, dodgerate, atkrate):
self.name = playername
self.hp = hp
self.mp = mp
self.atk = atk
self.xp = xp
self.dodgerate = dodgerate
self.atkrate = atkrate
class Enemy(Player):
def __init__(self, name, gold, maxhp, hp, mp, atk, xp):
self.name = name
self.gold = gold
self.maxhp = maxhp
self.hp = hp
self.mp = mp
self.atk = atk
self.xp = xp
class Items:
def __init__(self, name, quantity, description, price, weight):
self.name = name
self.quantity = quantity
self.description = description
self.price = price
self.weight = weight
Player = Player(playername, 1, 1, 1, 1, 25, 3)
print(Player.name + " has been created. ")
def raceselection():
raceinput = input("Do you float towards the TEMPLE, CAVE or FOREST?")
if raceinput == "TEMPLE":
print("You are now a high elf. High elves utlize a lot of magical power at the cost of being very frail.")
Player.hp = Player.hp + 24
Player.mp = Player.mp + 100
Player.atk = Player.atk + 50
print("You awaken from your slumber. Your room's walls are gold plated, and you rested on a flat board.")
print("Out the door, you see many elves with robes praying to some goddess.")
print("You walk out of your door and into the praying area. You are immediately greeted by a tall man.")
elif raceinput == "CAVE":
print("You are now an orc.")
Player.hp = Player.hp + 1000
Player.mp = Player.mp + 15
Player.atk = Player.atk + 50
print("cave")
elif raceinput == "FOREST":
print("You are now a human.")
Player.hp = Player.hp + 50
Player.mp = Player.mp + 25
Player.atk = Player.atk + 25
else:
print("You can't float there!")
raceselection()
raceselection()
inventory = []
def gameprompt():
global inventory
global zone
global movement
global restcounter
global searchcounter
if (movement == 5):
movement = movement - movement
zone = zone + 1
print("You have advanced to zone",zone,"!!!")
gameprompt()
if (zone == 1):
print("Welcome to the first zone! Easy enemies are here with not very good loot./fix grammar, add description of zone/")
elif (zone == 2):
print("Hey, it actually travelled to the second zone, awesome!")
elif (zone == 3):
print("No way would this actually work!")
prompt = input("Would you like to walk, search or rest?: ")
if (prompt == "walk"):
encounterchance = random.randint(1, 3)
if (encounterchance == 2):
if (zone == 1):
mobspawnrate = random.randint(1,3)
if (mobspawnrate == 1):
Enemy = Enemy("Blue SlimeBall", 50, 0, 25, 15, 25, 0.500)
print("You have encountered a " + Enemy.name + "!!!")
elif (mobspawnrate == 2):
Enemy = Enemy("Blue SlimeBall", 50, 0, 25, 15, 25, 0.500)
print("You have encountered a " + Enemy.name + "!!!")
elif (mobspawnrate == 3):
Enemy = Enemy("Blue SlimeBall", 50, 0, 25, 15, 25, 0.500)
print("You have encountered a " + Enemy.name + "!!!")
else:
movement = movement + 1
print("You have walked a step. You are now at ",movement," steps")
gameprompt()
elif (prompt == "search"):
if (searchcounter == 3):
print("You cannot search this area anymore! Wait until you reach the next zone!")
gameprompt()
else:
searchchance = random.randint(1, 5)
if (searchchance == 1 or 2 or 3 or 4):
searchcounter = searchcounter + 1
print(searchcounter)
print("You have found something!")
searchchance = random.randint(1,4)
if (searchchance == 1 or 2):
inventory.append(Items("Old Boot", 1, "An old smelly boot. It's a mystery as to who it belongs to...", 5, 50))
print("You have found a Boot!")
print(inventory)
elif(searchchance == 3):
inventory.append(Items("Shiny Boot", 1, "Looks like a boot that was lightly worn. You could still wear this.", 5, 50))
print(inventory)
print("You have found a Shiny Boot!")
elif(searchchance == 4):
inventory.append(Items("Golden Boot", 1, "It's too heavy to wear, but it looks like it could sell for a fortune!", 5, 50))
print("You have found a Golden Boot?")
print(inventory)
else:
searchcounter = searchcounter + 1
print(searchcounter)
print("You did not find anything of value")
gameprompt()
elif (prompt == "rest"):
if (restcounter == 1):
print("Wait until you reach the next zone to rest again!")
gameprompt()
else:
# Add a MaxHP value to the player later, and the command rest will give 25% of that HP back.
Player.hp = Player.hp + (Player.hp / 5)
print("You have restored ",(Player.hp / 5)," hit points!")
restcounter = restcounter + 1
gameprompt()
elif (prompt == "examine"):
print(inventory[1].name)
gameprompt()
gameprompt()
A list comprehension or map would work perfectly here:
print([item.name for item in inventory])
The comprehension iterates the list, and "replaces" each element in the list with whatever the part before for evaluates to. In this case, it's item.name.
° It actually doesn't replace the element in the original list. It evaluates to a new list full of replaced items.
I have been making a text-based game. I have gotten to a point where I am trying to do a battle. I have the enemy's hp going down but when I hit it again, the enemy's hp is back to what I had originally set it as. I hope these help.
while do != 'hit rat with sword' or 'run away':
enemyhp= 50
enemyattack= [0,1,3]
OldSwordattack= [0,1,4]
print('What do you do?(attack or run away)')
do=input()
if do == 'run away':
print('You can\'t leave mom like that!')
if do == 'hit rat with sword':
hit= random.choice(OldSwordattack)
if hit == OldSwordattack[0]:
print('Your swing missed')
print('Enemy\'s HP=' + str(enemyhp))
if hit == OldSwordattack[1]:
print('Your swing hit, but very lightly.')
enemyhp= enemyhp - 1
print('Enemy\'s HP=' + str(enemyhp))
if hit == OldSwordattack[2]:
print('Your swing hit head on!')
enemyhp= enemyhp - 4
print('Enemy\'s HP=' + str(enemyhp))
>In front of mom you see a giant, yellow-teethed rat.
>What do you do?(attack or run away)
>hit rat with sword
>Your swing hit, but very lightly.
>Enemy's HP=49
>What do you do?(attack or run away)
>hit rat with sword
>Your swing hit, but very lightly.
>Enemy's HP=49
>What do you do?(attack or run away)
You see? The above is the program running. I do not see why the value is not being changed.
because you are initializing it at the beginning of the iteration. So just move the
enemyhp= 50
before the while loop, like this
enemyhp= 50
while do != 'hit rat with sword' or 'run away':
Just for fun I've extended your example program to include some more advanced language constructs. May you find it entertaining: (Python 3)
import random
import re
def dice(s, reg=re.compile('(\d+d\d+|\+|\-|\d+)')):
"""
Executes D+D-style dice rolls, ie '2d6 + 3'
"""
total = 0
sign = 1
for symbol in reg.findall(s):
if symbol == '+':
sign = 1
elif symbol == '-':
sign = -1
elif 'd' in symbol:
d,s = [int(k) for k in symbol.split('d')]
for i in range(d):
total += sign * random.randint(1, s)
else:
total += sign * int(symbol)
return total
class Character():
def __init__(self, name, attack, defense, health, weapon):
self.name = name
self.attack = attack
self.defense = defense
self.health = health
self.weapon = weapon
def hit(self, target):
if self.is_dead:
print('{} is trying to become a zombie!'.format(self.name))
return 0
attack = self.attack + self.weapon.attack_bonus + dice('2d6')
defense = target.defense + dice('2d6')
if attack > defense:
damage = int(random.random() * min(attack - defense + 1, self.weapon.damage + 1))
target.health -= damage
print('{} does {} damage to {} with {}'.format(self.name, damage, target.name, self.weapon.name))
return damage
else:
print('{} misses {}'.format(self.name, target.name))
return 0
#property
def is_dead(self):
return self.health <= 0
class Weapon():
def __init__(self, name, attack_bonus, damage):
self.name = name
self.attack_bonus = attack_bonus
self.damage = damage
def main():
name = input("So, what's your name? ")
me = Character(name, 4, 6, 60, Weapon('Old Sword', 2, 4))
mom = Character('Mom', 2, 2, 50, Weapon('Broom', 0, 1))
rat = Character('Crazed Rat', 5, 2, 40, Weapon('Teeth', 1, 2))
print("You are helping your mother clean the basement when a Crazed Rat attacks. "
"You grab your Grandfather's sword from the wall and leap to defend her!")
actors = {me, mom, rat}
coward = False
killer = None
while (me in actors or mom in actors) and rat in actors:
x = random.choice(list(actors))
if x is mom:
mom.hit(rat)
if rat.is_dead:
killer = mom
actors -= {rat}
elif x is rat:
target = random.choice(list(actors - {rat}))
rat.hit(target)
if target.is_dead:
actors -= {target}
if target is mom:
print('Your mother crumples to the floor. AARGH! This rat must die!')
me.attack += 2
else:
print('Well... this is awkward. Looks like Mom will have to finish the job alone...')
else: # your turn!
print('Me: {}, Mom: {}, Rat: {}'.format(me.health, 0 if mom.is_dead else mom.health, rat.health))
do = input('What will you do? [hit] the rat, [run] away, or [swap] weapons with Mom? ')
if do == 'hit':
me.hit(rat)
if rat.is_dead:
killer = me
actors -= {rat}
elif do == 'run':
if me.health < 20:
actors -= {me}
coward = True
else:
print("Don't be such a baby! Get that rat!")
elif do == 'swap':
me.weapon, mom.weapon = mom.weapon, me.weapon
print('You are now armed with the {}'.format(me.weapon.name))
else:
print('Stop wasting time!')
if rat.is_dead:
if mom.is_dead:
print('The rat is gone - but at a terrible cost.')
elif me.is_dead:
print("Your mother buries you with your trusty sword by your side and the rat at your feet.")
elif coward:
print("The rat is gone - but you'd better keep running!")
elif killer is me:
print("Hurrah! Your mother is very impressed, and treats you to a nice cup of tea.")
else:
print('Your mother kills the rat! Treat her to a nice cup of tea before finishing the basement.')
else:
print('I, for one, hail our new rat overlords.')
if __name__=="__main__":
main()