Reputation: 636
I am new to python programming with MySQL connectivity. I am working on my grade 12 project which is on the Stock Market or Primary Market aka New issue Market. 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'
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()
Upvotes: 0
Views: 76
Reputation: 636
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)
Upvotes: 1