0

Almost finished Pset7 but I'm getting an error on Check50 that I don't understand. The log file says:

:( buy handles valid purchase

Log 
sending POST request to /login 
sending POST request to /buy 
exception raised in application: TypeError: unsupported operand type(s) for *: 'NoneType' and 'int' 

Here's my code for the index application:

def index():
    """Show portfolio of stocks"""
# User reached page via redirect
if request.method == "GET":

    # Lookup username and cash of current user and store in variables
    user = db.execute("SELECT username FROM users WHERE id = :user_id", user_id = session["user_id"])
    cash = db.execute("SELECT cash FROM users WHERE id = :user_id", user_id = session["user_id"])

    # Lookup current portfolio of user and store in variable sumshares
    sumshares = db.execute("SELECT stock, symbol, SUM(numshares), price, valuestocks FROM portfolio WHERE user_id = :user_id GROUP BY stock", user_id = session["user_id"])

    # Define variable for total value of all stocks and initialize to zero
    totalvaluestocks = 0

    # Update sumshares with latest stock prices
    for i in range(len(sumshares)):
        sumshares[i]['price'] = lookupindex(sumshares[i]['symbol'])
        sumshares[i]['valuestocks'] = sumshares[i]['price'] * sumshares[i]['SUM(numshares)']
        totalvaluestocks = totalvaluestocks + sumshares[i]['valuestocks']

        # Update to format USD
        sumshares[i]['price'] = usd(sumshares[i]['price'])
        sumshares[i]['valuestocks'] = usd(sumshares[i]['valuestocks'])

    # Calculate grand total
    grandtotal = cash[0]['cash'] + totalvaluestocks

    # format values to USD
    grandtotal = usd(grandtotal)
    cash[0]['cash'] = usd(cash[0]['cash'])
    totalvaluestocks = usd(totalvaluestocks)

    # Generate index page
    return render_template("index.html", user = user[0]['username'], cash = cash[0]['cash'], totalvaluestocks = totalvaluestocks, sumshares = sumshares, grandtotal = grandtotal)

# User reached route via POST (as by submitting a form via POST)
elif request.method == "POST":

    # Check for valid input
    try:
        extracash = float(request.form.get("extracash"))

    except ValueError:
        return apology("Please type in valid cash amount", 403)

    # Add cash to user
    current = db.execute("SELECT cash FROM users WHERE id = :user_id", user_id = session["user_id"])
    db.execute("UPDATE users SET cash = :cash WHERE id = :user_id", cash = current[0]['cash'] + extracash, user_id = session["user_id"])

    # Return user to home page
    return redirect("/")

Looking at the log file, there is a problem with the application trying to multiply two variables, one of which is of type 'NonType' and the other is an int. Since there is only one line of code in my whole program where I multiply two values, one of them being an int it should result from:

sumshares[i]['valuestocks'] = sumshares[i]['price'] * sumshares[i]['SUM(numshares)']

This line of code is meant to update the value of the stock with the current price info. So instead of showing the historic price (at time of purchase), it shows the current price.

In the buy section there is another multiplication, but that multiplication has no ints as input.

Also the buy operation works as expected, when I type in an existing stock, a valid number of shares and have enough cash. Can someone help me out with a suggestion or knows where this error might be coming from?

5
  • That does look like the only multiplication.....but neither operand is an int. Curious. Does the check50 log give additional information by any chance? Commented Nov 26, 2018 at 22:44
  • Maybe don't call lookup twice, but only once, in case first succeeds and second fails. An if always ending in return does not need an else, as anything after it already is reached only if that if did not trigger.
    – Blauelf
    Commented Nov 27, 2018 at 9:02
  • @DinoCoderSaurus: the log file at the top is the only info. But maybe the error does not come from the buy section but the index section. There is another multiplication, where one of the operands is an int.
    – JJuice
    Commented Nov 27, 2018 at 9:57
  • I have edited the question and included the index application, instead of the buy application.
    – JJuice
    Commented Nov 27, 2018 at 10:07
  • @Blauelf: thanks for the suggestion, calling it once and check after that if it succeeded solved the issue. But now I am getting a new error message :(..
    – JJuice
    Commented Nov 27, 2018 at 10:34

1 Answer 1

0

Calling the lookup function in buy only once and checking after that if it succeeded solved the issue, as suggested by Blauelf.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .