I have a list of namedtuples named Books
and am trying to increase the price
field by 20% which does change the value of Books
. I tried to do:
from collections import namedtuple
Book = namedtuple('Book', 'author title genre year price instock')
BSI = [
Book('Suzane Collins','The Hunger Games', 'Fiction', 2008, 6.96, 20),
Book('J.K. Rowling', "Harry Potter and the Sorcerer's Stone", 'Fantasy', 1997, 4.78, 12)]
for item in BSI:
item = item.price*1.10
print(item.price)
But I keep getting :
Traceback (most recent call last):
print(item.price)
AttributeError: 'float' object has no attribute 'price'
I understand that I cannot set the fields in a namedtuple. How do I go about updating price
?
I tried to make it into a function:
def restaurant_change_price(rest, newprice):
rest.price = rest._replace(price = rest.price + newprice)
return rest.price
print(restaurant_change_price(Restaurant("Taillevent", "French", "343-3434", "Escargots", 24.50), 25))
but I get an error with replace saying:
rest.price = rest._replace(price = rest.price + newprice)
AttributeError: can't set attribute
Can someone let me know why this is happening?