In the function buy when I am purchasing some stock, every query that i have mentioned on different conditions are getting implemented, I am unable to guess as why It is happening. Here is my code:
def buy():
"""Buy shares of stock"""
if request.method=="GET":
return render_template("buy.html")
else:
stock=request.form.get("stock").upper()
shares=float(request.form.get("shares"))
stockDict=lookup(stock)
if not stock:
return apology("Missing Stock")
elif not shares:
return apology("Missing Shares")
elif stockDict==None:
return apology("Invalid Symbol")
else:
price=float(stockDict['price'])
cashList=db.execute("SELECT cash, username from users WHERE id=?",session["user_id"])
currentCash= cashList[0]['cash']
username= cashList[0]['username']
if currentCash< price*shares:
return apology("You do not have enough money to buy the shares.")
else:
some=db.execute("SELECT id, stock, shares FROM stocks WHERE user=?", username)
if len(some)==0:
db.execute("INSERT INTO stocks(user, stock, shares) VALUES (?,?,?)",username, stock, shares)
else:
for row in some:
if row['stock']==stock:
address=row['id']
oldShares=row['shares']
db.execute("UPDATE stocks SET shares=:newShares WHERE id=:address", newShares=oldShares+shares, address=address)
else:
db.execute("INSERT INTO stocks(user, stock, shares) VALUES (?,?,?)",username, stock, shares)
return redirect("/")
The actual problem here is with the queries that I have written after the statementif len(some)==0
because even if there are conditional statements, the queries are getting implemented.