0

I am working on a CS50 task called "finance".

The problem is that I get an error when I want to check the integer value of the share amount and put it on a separate template for checking my code.

HTML input:

        <div class="form-group">
            <input autocomplete="off" autofocus class="form-control" name="shares" placeholder="Shares" type="number">
        </div>

Python:

        elif int(request.form.get("shares")) < 1:
            return apology("please enter a positive amount of shares")
        else:
            return render_template("bought.html", name=x["name"], symbol=x["symbol"], price=x["price"], shares=x["shares"])

Error:

return f(*args, **kwargs)
File "/home/ubuntu/finance/application.py", line 69, in buy
return render_template("bought.html", name=x["name"], symbol=x["symbol"], price=x["price"], shares=shares)
KeyError: 'shares'

My idea was to convert the Shares from int to a string.

HTML/Jinja:

    Amount of Shares: {{shares}}

But somehow the error still remains. Do you have any further ideas?

2
  • The error that comes from line 69 in buy says the dict named x does not have a key named "shares". Since x appears to be info from lookup function, likely there is a problem with the code that creates the "shares" key in x. Jun 21 at 18:56
  • Your hint was really helpful. I got the solution thanks.
    – kiwirolle
    Jun 22 at 13:02

You must log in to answer this question.

Browse other questions tagged .