I am doing CS50 Web track finance buy and this is my code which gives the error given below The error which it is giving is confusing and I dont understand what it is saying as well as how to correct my code. Any help wpuld be appreciated.
def buy():
"""Buy shares of stock"""
if request.method == "POST":
symbol = request.form.get("Symbol")
quote = lookup(symbol)
# Checking if Quote is available
if not quote:
return apology("Quote not Found",403)
else:
shares = request.form.get("Shares")
# Get current user cash
rows = db.execute("SELECT * FROM users WHERE id=?", session["user_id"])
cash = rows[0]["cash"]
print(cash)
amount = float(shares)*quote["price"]
if cash < amount:
return apology("NOT ENOUGH CASH",403)
else:
cash -= amount
# Add to transactions
db.execute("INSERT INTO transactions (user_id,symbol,price,shares,amount) VALUES(:user_id,:symbol,:price,:shares,:amount)",user_id=session["user_id"],symbol=quote["symbol"],price=quote["price"],shares=shares,amount=amount)
# update cash in users
db.execute("UPDATE users SET cash = :cash WHERE id=:user_id",user_id =session["user_id"],cash=cash)
return redirect("/")
else:
return render_template("buy.html")
Error
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/ubuntu/finance/helpers.py", line 34, in decorated_function
return f(*args, **kwargs)
File "/home/ubuntu/finance/application.py", line 57, in buy
quote = lookup(symbol)
File "/home/ubuntu/finance/helpers.py", line 44, in lookup
response = requests.get(f"https://cloud-sse.iexapis.com/stable/stock/{urllib.parse.quote_plus(symbol)}/quote?token={api_key}")
File "/usr/local/lib/python3.7/urllib/parse.py", line 850, in quote_plus
string = quote(string, safe + space, encoding, errors)
File "/usr/local/lib/python3.7/urllib/parse.py", line 834, in quote
return quote_from_bytes(string, safe)
File "/usr/local/lib/python3.7/urllib/parse.py", line 859, in quote_from_bytes
raise TypeError("quote_from_bytes() expected bytes")
TypeError: quote_from_bytes() expected bytes
INFO: 192.168.239.121 - - [29/Dec/2020 13:18:22] "POST /buy HTTP/1.0" 500 -
INFO: 192.168.239.121 - - [29/Dec/2020 13:18:23] "GET /static/styles.css HTTP/1.0" 200 -
INFO: 192.168.239.121 - - [29/Dec/2020 13:18:23] "GET /static/favicon.ico HTTP/1.0" 200 -
Aucun commentaire:
Enregistrer un commentaire