How to use classes to create attacks in python - python

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")

Related

Trying to get game to level up and use new enemy

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`

Object Oriented Programming assignment [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Basically, the assignment is to create code that will allow a game to run.
the code that I have written is:
class Character(object):
def __init__(self, name, health):
self.name = name
self.health = health
def take_damage(self, damage):
self.damage = damage
self.health = self.health - damage
return self.health
def __str__():
return self.name, "(health = " + self.health + ")"
class Hero(Character):
def __init__(self, name, health):
super().__init__(name, health)
self.__inventory = list()
def restore_health(self, heal):
self.heal = heal
self.health = self.health + heal
def add_inventory(self, inventory):
self.__inventory.append(inventory)
def remove_inventory(self, inventory):
self.__inventory.remove(inventory)
def get_inventory(self, inventory):
return self.__inventory
class Enemy(Character):
def __init__(self, name, health, damage):
super().__init__(name, health)
self.damage = damage
def main():
han = 'Han'
zombie = 'Zombie'
werewolf = 'Werewolf'
print(Hero(han, 40))
print(Enemy('Zombie', 20, 15))
print(Enemy('Werewolf', 15, 10))
if __name__ == '__main__':
main()
For reference, the code that of the game is this, but I know there's nothing wrong with it:
HW9 is the title of my code seen above
from HW9 import *
import random
def check_for_health_elixir(hero):
random_chance = random.randint(0,5)
if random_chance == 5:
health_amt = random.randint(2,5)
print("You have found a health elixir.")
print("Drinking it restores your health by " \
+ str(health_amt) + ".")
hero.restore_health(health_amt)
print("Your current health is " + str(hero.health) + ".\n")
def run(hero, enemy):
damage = random.randint(0, enemy.damage//2)
print("The " + enemy.name + " inflicted a damage of " \
+ str(damage) + " as you ran away.")
health = hero.take_damage(damage)
print("Your current health is " + str(health) + ".\n")
def fight(hero, enemy):
winner = random.randint(0,1)
if winner == 0:
print("You have defeated the " + enemy.name + ".")
else:
print("You have lost the fight. The " \
+ enemy.name \
+ " has inflicted a damage of " \
+ str(enemy.damage) + ".")
health = hero.take_damage(enemy.damage)
print("Your current health is " + str(hero.health) + ".\n")
def main():
hero = Hero('Elsa', 40)
goblin = Enemy('Goblin', 10, 5)
dragon = Enemy('Dragon', 30, 10)
ogre = Enemy('Ogre', 20, 8)
wizard = Enemy('Wizard', 20, 10)
enemies = {goblin, dragon, ogre, wizard}
print("You are the hero " + hero.name + ". Your current health is " \
+ str(hero.health) + ".\n")
game_over = False
while not game_over:
health = check_for_health_elixir(hero)
enemy = random.sample(enemies, 1)[0]
print("You have encountered a " + enemy.name + ".")
action = input("Do you want to run or fight? ").strip()
while action != "run" and action != "fight":
action = input("Do you want to run or fight?").strip()
if action == "run":
run(hero, enemy)
elif action == "fight":
fight(hero, enemy)
if hero.health <= 0:
print("You are dead!\nGame over.")
game_over = True
if __name__ == '__main__':
main()
What my code is supposed to print out is:
Start:
Han (health=40)
Zombie (health=20)
Warewolf (health=15)
Battle 1:
Han (health=30)
Warewolf (health=5)
Battle 2:
Han (health=15)
Zombie (health=0)
Restore Health:
Han (health=20)
Inventory:['gold coin', ‘candle']
The problem I am encountering is that it says:
print(Hero(han, 40))
TypeError: __str__() takes 0 positional arguments but 1 was given
This comes from the def main() at the end of my code, but I'm not sure what I'm doing wrong, there's still more code to write in but I want to figure out what I'm doing wrong here first.
The issue is that you forgot to add the self argument in your _str_ function. All functions not marked as #staticmethod
in classes must have the self argument. So to correct it, you just need to change it to:
def __str__(self):

how to sort a list by a attribute [duplicate]

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

Variable X not updating when variables that should effect X change

I am a beginner at python. As most beginners, I am trying to make a text based rpg. I have finally gotten to the leveling up system and am at another roadblock. When the character levels up, they get to allot x skill points to a skill (add x to a variable x times). two of these variables effect the health of the character, however, when these two variables are changed by the user, the health variable stays the same. I have simplified my code for ease of reading (as my prose above is not that clear to begin with lol):
class Char:
def __init__(self, x, y):
self.str = x
self.con = y
self.hp = (self.con + self.str) / 2
player = Char(20, 20)
def main(dude):
print("strength: " + str(dude.str))
print("constitution: " + str(dude.con))
print("hp: " + str(dude.hp))
print("------")
action = input("press 1 to change str, 2 to change con")
if action == "1":
dude.str = dude.str + 10
main(dude)
elif action == "2":
dude.con = dude.con + 10
main(dude)
else:
main(dude)
main(player)
although these variables can increase by increments of ten in this situation, the hp remains at 20
how can I remedy this problem?
Thanks!
Whenever the Char's attributes are updated, the code needs to re-compute the HP.
All this sort of code is best put inside the Char object:
class Char:
def __init__(self, x, y):
self.str = x
self.con = y
self.setHP()
def __str__(self):
text = "strength: " + str(self.str) + "\n" +\
"constitution: " + str(self.con) + "\n" +\
"hp: " + str(self.hp)
return text
def setHP(self):
self.hp = (self.con + self.str) / 2
def adjustStr(self, amount):
self.str += amount
self.setHP()
def adjustCon(self, amount):
self.con += amount
self.setHP()
def main(dude):
print(str(dude))
print("------")
action = input("press 1 to change str, 2 to change con")
if action == "1":
dude.adjustStr(10)
main(dude)
elif action == "2":
dude.adjustCon(10)
main(dude)
else:
main(dude)
player = Char(20, 20)
main(player)
The hp attribute does not change when the str or con change. The only time it is set is in the constructor. You could define an update method to Char like this:
class Char:
def __init__(self, x, y):
self.str = x
self.con = y
self.update()
def update(self):
self.hp = (self.con + self.str) / 2
and call it at the end of main:
def main(dude):
print("strength: " + str(dude.str))
print("constitution: " + str(dude.con))
print("hp: " + str(dude.hp))
print("------")
action = input("press 1 to change str, 2 to change con")
if action == "1":
dude.str = dude.str + 10
main(dude)
elif action == "2":
dude.con = dude.con + 10
main(dude)
else:
main(dude)
dude.update()
Because you evaluate the hp attribute only in the __init__() method, i. e. only in your statement
player = Char(20, 20)
The most quick fix (not a very nice one, but a very simple) is to create a new instance of Char after each change:
if action == "1":
dude.str = dude.str + 10
dude = Char(dude.str, dude.con) # new dude (with current str and con)
main(dude)
elif action == "2":
dude.con = dude.con + 10
dude = Char(dude.str, dude.con) # new dude (with current str and con)
main(dude)
You have to update the hp each time, since you used self.str and self.con only once to compute hp and stored it independently from self.str and self.con. You could use a get_hp for that.
class Char:
def __init__(self, x, y):
self.str = x
self.con = y
def get_hp(self):
return (self.con + self.str) / 2
player = Char(20, 20)
def main(dude):
print("strength: " + str(dude.str))
print("constitution: " + str(dude.con))
print("hp: " + str(dude.get_hp()))
print("------")
action = input("press 1 to change str, 2 to change con")
if action == "1":
dude.str = dude.str + 10
main(dude)
elif action == "2":
dude.con = dude.con + 10
main(dude)
else:
main(dude)
I don't agree with the other answers, as the best way for me would have been to use a property. Properties work just like getters, but has the benefit to allow it to be dynamically calculated, which is exactly what you are looking for.
class Char:
def __init__(self, x, y):
self.str = x
self.con = y
#property
def hp(self):
# todo handle negative hp!
return (self.con + self.str) / 2.
def test_hp():
player = Char(20, 20)
assert player.hp == 20
def test_hp_with_changes_to_con_or_str():
player = Char(20, 20)
player.con += 10
assert player.hp == 25
player.str += 10
assert player.hp == 30
Also, as you can see, it is better to just write a test than to test stuff with output to stdout.
scratch_12.py .. [100%]
========================== 2 passed in 0.20 seconds ===========================
The above hp property is read-only, but you can make a setter for it as well if needed. Read up on property
PS: also, use full names, like Character as it is better to be explicit than implicit :)

Is there a way to print all of a certain element of a class that is in a list?

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.

Categories