Reputation: 21
I am quite new to Python learning how it works this is a program I've made and I cant seem to reduce the quantity
How do you subtract a specific number from a single element in a list?
import csv
stockfile = open("Stock list.txt")
csvf = csv.reader(stockfile,delimiter = ",")
#Sets csvf vaiable to open text file
#Delimiter is what seperates your values
code = []
item = []
price = []
quantity = []
quantity = list(map(int, quantity))
for row in csvf:
code.append(row[0])
item.append(row[1])
price.append(row[2])
quantity.append(row[3])
usersearch = input("Please enter a 7-digit code: ")
#Asks the user to enter the unique item codes
while usersearch not in code:
print("This is not a valid 7-digit code")
usersearch = input("Please enter a 7-digit code: ")
if usersearch in code:
print("The code has been found")
index = code.index(usersearch)
print("The item you have chosen is: %s"%item[index])
print("The price is: £%s"%price[index])
print("There are %s left in stock"%quantity[index])
quantity[index] -= 1
I want to reduce the quantity[index] by 1
When I use quantity[index] -= 1
I get an error:
TypeError: unsupported operand type(s) for -=: 'str' and 'int'
These are the contents of the file:
0000001,PS4,300,50
0000011,Xbox One,300,50
0000111,Need for Speed,40,20
0001111,Just Cause 3,45,24
0011111,Dualshock 4,48,30
Never mind I figured it out, the quantity=list(map(int, quantity))
had to come just before the quantity[index] -= 1
Upvotes: 1
Views: 551
Reputation: 21269
Python has a strong concept of 'names'. Using your example:
a = [12,34,43,21,11]
a
is a 'name' that is pointing to a list of five numbers, or, more generally, a
points to a place in memory where some data is being held - in this case an array. When you 'index into' an list, you are, in effect, being more specific with the name you're using. So the name a[4]
points to a place in memory where an integer is held, 11
.
So when you do this:
a[4] = "bob"
You are saying 'change the place in memory pointed to by the name a[4]
to "bob"'. When you do this:
a[4] = a[4] - 3
You are saying 'change the place in memory pointed to by the name a[4]
and change it to the result of the following evaluation: a[4] - 3
.' That evaluation first looks to see what is already in a[4]
, then subtracts three. In this case that is 8
and that result is assigned to the location in memory referred to by a[4]
.
Note that 'getting specific' with names is dependent on what 'datatype' the original name is. Since your a
is a list, Python knows how to index into it. Here is Python (in the REPL) trying to index into a few other common types:
>>> s = "abcde"
>>> s[4]
'e'
>>> b = 5
>>> b[5]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not subscriptable
>>> c = { 'a':1, 'b':2, 'c':3, 'd':4, 'e':5 }
>>> c[4]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 4
>>> c['d']
4
Note that in that last one, the key d
worked but 4
did not. It is important to know what you're indexing into when you're trying to get specific with what you're doing!
Upvotes: 1
Reputation: 1125388
You could use augmented assignment, -=
:
a[4] -= 3
Or just re-assign the result of a subtraction back to the index (which is what -=
does anyway):
a[4] = a[4] - 3
Upvotes: 5