簡體   English   中英

如何減去列表中的項目值和變量python

[英]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)

當我運行此代碼時,我收到錯誤消息:

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

我如何做到這一點,以便它減去變量。 我讀到將int放在最前面會允許加法減法,依此類推,但事實並非如此。 請幫我!

這里的問題是stockslist.index(x)[1]它返回x列表的索引值(數組下標值)。

請更新此

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM