-3

Currently I'm testing something in python and trying to figure out if I can change a value in another file. Currently I have this written down :

from items import items

def changingitemamount(name, value):
    print(items[name][6])
    items[name][6] = items[name][6] + int(value)
    print(items[name][6])

def changingitemamounttext():
    name = input("What do you want to change?")
    value = input("How much do you want to add?")
    changingitemamount(name,value)

But whenever I run it and go to add the value i get this error.

items[name][6] = items[name][6] + int(value)

TypeError: 'tuple' object does not support item assignment
Randolph
  • 35
  • 5
  • What's `items` in this import? – Padmal Jul 29 '19 at 07:26
  • 1
    Possible duplicate of ['tuple' object does not support item assignment](https://stackoverflow.com/questions/7687510/tuple-object-does-not-support-item-assignment) – Sayse Jul 29 '19 at 07:26
  • what does your `print(items[name][6])` print out? – Mig B Jul 29 '19 at 07:27
  • `items[name]` is clearly a tuple, use a list instead – Sayse Jul 29 '19 at 07:27
  • 1
    check https://stackoverflow.com/questions/11458239/python-changing-value-in-a-tuple to see how to change value in a tuple – s.singh Jul 29 '19 at 07:27
  • `items[name][6] += int(value)` is equivalent of `items[name][6] = items[name][6] + int(value)` – Rahul Jul 29 '19 at 07:29
  • 1
    `items[name][6]` is a tuple (immutable data-type) so that is why you get `TypeError: 'tuple' object does not support item assignment` – dejanualex Jul 29 '19 at 07:31
  • @Sayse Even after changing it to a tuple I still get an error. This time around its "print(items(name)[6]) TypeError: 'dict' object is not callable" – Randolph Jul 29 '19 at 07:45
  • @Sayse Dah sorry. Messed up and typed tuple instead of list. Still. I swapped everything to parethesis since i made this mistake before. And my lists are well. Already lists. – Randolph Jul 29 '19 at 08:24

1 Answers1

0

A tuple is immutable please convert them into a list and do your operations and then you can revert them back to tuples.

Like :

x = (4,5)       
listNumbers = list(x)  
print(listNumbers)
y = tuple(listNumbers)
print(x)

Hope this helps.