Search code examples
pythonmultidimensional-arraysubtraction

How do I subtract the value of a item in a list and a variable python


from builtins import int
apple = 'apple'
stockslist = [['apple', 174] , ['amazon', 2558] , ['google', 876] ,    ['facebook', 145] , ['square', 61]]
usersetup = input('What is your username?')
balance = (int(input("How much would you like to deposit")))
userbalance = ([usersetup, balance])
stock =  input('what stock would you like to buy')
operation =  input('don trades or normal trading')
if operation == 'don trades':
  stockdeposit = (int(input("Pick a number between 10 and 200?")))
x = [x for x in stockslist if stock in x][0]

print(4-2)
print(stockslist.index(x)[1] - stockdeposit)

When I run this code I receive the error:

print(stockslist.index(x)[1] - stockdeposit)
TypeError: 'int' object is not subscriptable.

How do I make it so it subtracts the variables. I read that putting int infront would allow addition subtraction and so on but it didn't. Please help me!


Solution

  • Problem here is stockslist.index(x)[1] it return an index value(array subscript value) of x list.

    Please update this

    print(stockslist[stockslist.index(x)][1] - stockdeposit)