So I want to make a class that functions as a bag. I simply want to make 2 functions in that bag: one function that adds an item in the bag and adds a specified amount and a function that takes out an item and the specified quantity of that item.
For the adding an item function, I want to be able to add a new item with a certain quantity and if that item is already in the list, the quantity put in is added to the existing quantity of that item.
For the taking out an item function, if the quantity that wants to be taken out of the bag is the same as the amount already in the bag, the item is deleted from the list. If the amount that wants to be taken out is less than the amount in the bag, the quantity of that item decreases. If the quantity that wants to be taken out is more than what is in the bag, there is a simple error message. And of course, if the item that wants to be taken out is not in the list, there is an error message.
The bag uses a list to store the items. I want the items to use a dataclass to store the quantity and the name of the item. I made this already:
from dataclasses import dataclass
@dataclass
class Item:
amount: int
item1: str
class Bag:
def __init__(self):
self.liste_item = []
def add_item(self, item, quantity):
if item in self.liste_item:
Item.amount += quantity
else:
self.liste_item.append(Item(quantity, item))
print(self.liste_item)
def take_item(self, item, quantity):
if item in self.liste_item:
if Item(quantity, item).amount == quantity:
del item
elif Item(quantity, item).amount < quantity:
print(f"You do not have {quantity} {item} in your bag.")
else:
Item.amount -= quantity
else:
print(f"{item} is not in your bag.")
print(self.liste_item)
This code does not work. The problem is I don't know how to use and change a specific variable's attributes from a dataclass. In this case it's changing the quantity of a specific item. The other problem I have is I don't know how to say "If this quantity of this item is in the bag..." or even the line:
if item in self.liste_item:
In this line I want to check whether the specific item is in the list. The problem is that it simply doesn't work and the code thinks that the item which is in the bag, is not in the bag. It only works when I make the change:
if item not in self.liste_item:
Which makes no sense to me. Please help me with this.
Item.amount
isn't a class attribute; it's just a quasi-declared instance attribute. The purpose of the dataclass
decorator is to use the contents of the class
statement in a fairly invasive way to create the "actual" class used later in your program. Part of that process is automatically generating Item.__init__
along the lines of
def __init__(self, amount, item1):
self.amount = amount
self.item1 = item1
As such, you should not be adding anything to Item.amount
directly. You need to find the corresponding instance in self.liste_item
and update it.
from dataclasses import dataclass
@dataclass
class Item:
amount: int
item1: str
class Bag:
def __init__(self):
self.liste_item: list[Item] = []
def add_item(self, item_name: str, quantity: int):
for item in self.liste_item:
if item.item1 == item_name
item.amount += quantity
break
else:
# Create a new Item instance if no match was found.
self.liste_item.append(Item(quantity, item_name))
...
take_item
must be rewritten similarly.
class Bag:
bag = {}
def add_item(self, new_item, quantaty):
if new_item in self.bag:
self.bag[new_item] += quantaty
else:
self.bag[new_item] = quantaty
def remove_item(self, item, quantaty):
if item in self.bag:
if quantaty > self.bag[item]:
print(f"Only {self.bag[item]} of {item} left. Cant substract {quantaty}")
else:
self.bag[item] -= quantaty
else:
pass
Kevin = Bag()
Kevin.add_item("sword", 10)
print(Kevin.bag)
Kevin.add_item("sword", 10)
print(Kevin.bag)
Kevin.add_item("stone", 10)
print(Kevin.bag)
Kevin.remove_item("stone", 5)
print(Kevin.bag)
Kevin.remove_item("stone", 6)
print(Kevin.bag)
Clould be like that. U can try it out. I just coded this for you Im using a Dictonary to safe all. Hope u can see how u can access the variable:) Got ua picture cuz im to dump to get it here right xD
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.