Hot answers tagged

2

symbol is a list of dictionaries; that is what execute returns. Therefore the first result returns each dictionary in the template for symbol in symbol loop. Similarly, symbol[0]["symbol'} returns the string "AAPL", so the second result returns each character from the string in the for loop. There are any number of ways to transform the list ...


2

So my first question is, where does it come from? It's either bootstrap or the browser. check50 does not use the front end to make the API calls. Therefore any limits put on the html, while GREAT for the user, might not satisfy the spec, as evidenced by this problem. Keep the "check" in the html, it's good user interface (IMO). Add the necessary ...


1

Assume it is this line that produces error: quantity_owned = int((db.execute ("SELECT quantity FROM user_stocks WHERE user_id = ?"), session['user_id'])[0]) There is a ) in the wrong place. session['user_id'] is an argument to the execute call. Spoiler: There are other problems ahead


1

I'm new to this, but am working on this PSET myself at the moment. Here's what I think the problem is (feel free to roast me if it's not! ;)): lookup returns a dictionary with 3 keys: 1.) name 2.) price 3.) symbol. You're trying to use a key called "stock". Try using: price = dictionary["price"] Hope that solves it. Best of luck with the ...


1

Since the quote spec calls for an element named "symbol", and this if not request.form.get("stock"): is apparently working, it looks like the element is not named properly in the html. FYI to change "I think" to "I know". ls -al helpers.py (in the finance directory) should return -rw------- 1 ubuntu ubuntu 1748 Apr ...


1

Have a look at what your index renders if user buys 2 shares of something. I suspect it will show the price twice and not "total value for each holding". The usd function returns a string, so price will be treated by database as a string because price = usd(price). (sqlite uses dynamic typing, more detail can be found in the doc) python will gladly ...


1

This hash = generate_password_hash("password") is a problem. It is hashing the string literal "password". (Try to login using "password" as the password; it should work). Perhaps you meant to hash the variable password.


1

If the symbol entered is invalid, this line price = search["price"] gives error "TypeError: 'NoneType' object is not subscriptable" because lookup returns None. Code will give Value error here shares = int(request.form.get("shares")) if shares is non-numeric. Further, casting shares to int is not sufficient to ensure that shares ...


1

The problem isn't lookup. The problem is portfolio. There are exactly two keys in the portfolio dictionary, "current_price" and "holding_total". They are set to the values in each row in sequence, and end up with the values from the last row. Instead of adding a new dictionary, consider adding new keys for each row in rows.


1

It is much better UI/UX to add the constraints to the front end. Alas, check50 is verifying that the code performs as instructed in the spec. The same directive is given for buy and sell routes. From the spec: Require that a user input a number of shares, implemented as a text field whose name is shares. Render an apology if the input is not a positive ...


1

Add value = "{{ stocks }}" to the symbol input element in buy.html. There are some problems in the GET branch of the buy route: To retrieve values from a GET request, use request.args instead of request.form (i.e. request.args.get('symbol_buy')) Beware discrepancy symb sym If request.args.get('symbol_buy') is None, you may want to send the empty ...


1

The problem is the double double-quotes. The first phrase is: "SELECT SUM(size) AS " the second phrase is: " FROM history WHERE id = ? AND symbol = ?" and Total is in no-man's land. It is not necessary to enclose table/column aliases with delimiters, unless they contain special characters. If that were the case, Total would need a ...


1

You are iterating over a variable called holdings: {% for stock_holdings in holdings %} But you are not passing any such variable to the template: return render_template("index.html", stock_holdings = stock_holdings, cash = usd(cash), total_funds = usd(total_funds)) So there is nothing there to iterate, so nothing is displayed.


1

From the doc for execute in CS50 Library for Python Returns for SELECT, a list of dict objects, each of which represents a row in the result set The argument to the INSERT query needs to be a specific element from the SELECT result, eg stocks[0]['SUM(shares)']


1

Quote and stocks are one and the same. So you should keep only one of them. Your first error takes place at price = quote.get("price"). This line is correct but in the wrong place. Once you have checked if quote is not None, you should get the price, not before checking. This should resolve your first error. As your code shows, you know that shares ...


1

The issue is that once you sell some stock, your profile_det (that you display on your index page) price values no longer have 2 decimal places. For example, if I buy 4 shares of a stock worth $28.00 each, your profile_det will be: [{'Symbol': 'AAAA', 'Name': 'AAAA Test', 'Shares': 4, 'Price': '$28.00', 'Total': '$112.00'}] But if I then sell 2 of those ...


1

This sql SELECT symbol, shares, price FROM transactions WHERE user_id = :user_id GROUP BY user_id, symbol uses a group by. But there is no aggregate function for the "grouping". Read up on the sqlite aggregate functions; you are interested in sum and/or total.


1

added_user = db.execute("INSERT INTO users (username, hash) VALUES(:username, :password_hash)", username=username, password_hash=password_hash) is going to fail if the username is taken, as you've seen. To handle this, use a try except to catch and handle it, rather than just checking if your result is 0.


1

The problem is simple. Your placeholder for hash in the sql insert statement is hash but then you have used hash_p instead of hash when assigning the value. Hope this helps. If it does then please check the tickmark.


Only top voted, non community-wiki answers of a minimum length are eligible