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)
运行此代码时,我收到错误消息:
print(stockslist.index(x)[1] - stockdeposit)
TypeError: 'int' object is not subscriptable.
如何使它减去变量。我读到将int放在最前面会允许加法减法,依此类推,但事实并非如此。请帮帮我!
答案 0 :(得分:1)
这里的问题是stockslist.index(x)[1],它返回x列表的索引值(数组下标值)。
请更新此
print(stockslist[stockslist.index(x)][1] - stockdeposit)