0

I have been using some of the html from the cs50 finance example page: https://finance.cs50.net/buy (which is permitted by CS50) I think it may be at the root of the problem. Here's the relevant html code for my BUY page:

<form action="/buy" method="post">
<div class="form-group">
    <input autocomplete="off" autofocus="" class="form-control" name="symbol" placeholder="Symbol" type="text">
</div>
<div class="form-group">
    <input autocomplete="off" class="form-control" min="1" name="shares" placeholder="Shares" type="number">
</div>
<button class="btn btn-primary" type="submit">Buy</button>

I think the "form-control" and min="1" limits the user to inputting valid numbers of shares.

In any case, my page shows an alert when user tries to enter an invalid/fractional amount shares, like so:

alert on buy page

What I am not sure about is where this "enter valid value" response comes from. So my first question is, where does it come from? Is this part of Bootstrap?

In any case, the alert does its job, and prevents the user from entering an invalid value.

HOWEVER, Check50 claims:

check50 output

So my second question is, does the alert send a "200" message, and if so, how do I change it to send a "400" message?

And if not, what else could the problem be?

Here's how the relevant part of my Buy function looks:

@login_required
def buy():
    
    if request.method == "POST":
        stock = lookup(request.form.get("symbol"))

        # Ensure valid symbol was submitted
        if not stock:
            return apology("Sorry! That symbol doesn't exist", 400)

        shares = int(request.form.get("shares"))

I think I could solve the problem by getting rid of the min=1 and then putting an if statement in my function along the lines of:

if not shares or (shares % 1 !=0) or shares < 0:

But I much prefer the current way it works, if only CS50 were happy with it! :P

Much obliged for any help you can provide! :)

1
  • OK, I've worked now (typically 2 secs after completing this question) that the Alert is coming up because I'm using the "type=number" but the question still stands. Can I not do this and make Check50 happy? And if not, why not?
    – cormacl
    Jan 21 at 20:41
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 check to the flask route.

1
  • Thanks @DinoCoderSaurus! Will do so!
    – cormacl
    Jan 21 at 22:21
0

So as DinoCoderSaurus kindly pointed out, the HTML was good. But due to the way Check50 works I needed some code in the flask route (application.py) as well.

The "type=number" in the HTML input box on the form restricts the user to inputting correct values, so I kept that. BUT I also added the following code, to satisfy Check50:

        shares = (request.form.get("shares"))
        try:
            int(shares)
        except ValueError:
            return apology("Enter valid amount of shares", 400)

        shares = int(shares)
        if shares < 1:
            return apology("Enter valid amount of shares", 400)
3
  • This code will still allow fractional shares. So it is best to check for the fraction part as well.
    – Vsjain
    Jan 22 at 12:06
  • Hi Vsjain, no, if you try to convert it to int() it returns a value error for a fractional share amount. Like this: ValueError: invalid literal for int() with base 10: '1.15'. So it works fine. But I think it's only because the input is passed in string format. I think passing a float into int() would return an integer successfully i.e. 1.15 would be changed to 1.
    – cormacl
    Jan 25 at 12:40
  • Ah yes. The value error exception will handle it.
    – Vsjain
    Jan 25 at 13:02

You must log in to answer this question.

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