Search code examples
pythonindentationcs50

I keep getting an indent error no matter what I do, even when there is no indentation


I cannot figure this one out, no matter what I do. I keep getting the Unexpected Indent Error, no matter what I do with the spacing.

I've spent two days trying to fix it myself and find someone who can explain what is going on. I have included a screenshot of my CS50 IDE because I feel that it best shows the error I am having.

Original error

@app.route("/sell", methods=["GET", "POST"])
@login_required
def sell():

   if request.method == "POST":

       quote = lookup(request.form.get("symbol"))

       if quote == None:
           return apology ("invalid symbol", 400)


        try:
            shares = int(request.form.get("shares"))
        except:
            return apology("shares must be positive", 400)

Updated error


Solution

  • You need to 'close' the try block with a except, i.e:

    try:
        shares = (int(request.form.get("shares"))
    except Exception as err:
        print(err)
    

    Or skip the try, and use try-except when you know what type of error to expect, i.e:

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

    More information on try-except blocks is here.


    So the error about indentation is misleading. I have been there too :(