简体   繁体   English

- 不支持的操作数类型:“int”和“list”

[英]unsupported operand type(s) for -: 'int' and 'list'

I am new to python programming with MySQL connectivity.我是 python 编程与 MySQL 连接的新手。 I am working on my grade 12 project which is on the Stock Market or Primary Market aka New issue Market.我正在从事我的 12 年级项目,该项目位于股票市场或一级市场,即新发行市场。 This is the Purchase Module of my project.这是我项目的购买模块。

If I run this code separately in pieces it runs without error but after putting it into combining all this error occurred.如果我分段运行此代码,它运行时不会出现错误,但在将其合并后会发生所有这些错误。

unsupported operand type(s) for -: 'int' and 'list' - 不支持的操作数类型:“int”和“list”

I don't know why it is happening我不知道为什么会这样

def converl(results,l):
#convert tuple into list
    for x in results:
        for z in x:
            l.append(z)
    return(l)



import mysql.connector as mycon
mydb = mycon.connect(user = 'root',
                         password = '',
                         host = 'localhost',
                         database = 'stockmarket')
cname =input('enter name of company to purchase shares ')
uname = 'amit'
sql = "select cname from companies"
cursor = mydb.cursor()
cursor.execute(sql)
results = cursor.fetchall()
l=[]       
t =  converl(results,l)
if cname in l:    
    try:

        csql = "select (purchased_shares) from purchase where cname = '%s'"%(cname)
        cursor = mydb.cursor()
        cursor.execute(csql)
        cresult = cursor.fetchall()
        s = len(cresult)
        res = []
        for i in cresult:
            t_sum = 0
            for j in i:
                t_sum += j
                res.append(t_sum)
                t = 0
                for z in range(0,s):
                    t = t + res[z]

        zsql = "select (issued_shares) from companies where cname = '%s'"%(cname)
        cursor.execute(zsql)
        zresult = cursor.fetchone()
        y = zresult[0]

        #finding shares left
        share_left = y - t
        print(share_left)
        if share_left>0:
            print(share_left,'available shares')

            no_of_shares = int(input('enter no of shares to be purchased '))

            if no_of_shares>zresult[0]:
                print('purchase shares should be equal to or less than shares left')
            else:
                csql = "select cno from companies where cname ='%s'"%(cname)
                cursor.execute(csql)
                cresult = cursor.fetchone()

                psql = "select rate from companies where cname ='%s'"%(cname)
                cursor.execute(psql)
                presult = cursor.fetchone()

                     #find total_paid
                total_paid = presult[0] * no_of_shares
                print(total_paid)

        #Insert into purchase
                sql = "insert into purchase (cno,username,cname,purchased_shares,total_paid)  values('%d','%s','%s','%d','%d')"%(cresult[0],uname,cname,no_of_shares,total_paid)
                cursor = mydb.cursor()
                cursor.execute(sql)

                #update companies table
                comsql = "update companies set sub_shares  = sub_shares + '%d' where cname ='%s'"%(no_of_shares,cname)
                cursor = mydb.cursor()
                cursor.execute(comsql)
                mydb.commit()
                print(no_of_shares,'Shares purchased')
                lnmenu()

                 #Insert into purchase


        else:
            print('No more shares left')
            tryagain()


    except Exception as expt:
        print(expt)

else:
    print('company not found')
    tryagain()

Finally, I solved it最后,我解决了

Just change this改变这个

s = len(cresult)
res = []
for i in cresult:
    t_sum = 0
    for j in i:
        t_sum += j
        res.append(t_sum)
t = sum(res)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM