0

I am new to python and am very confused by this error message:

AttributeError: 'NoneType' object has no attribute 'startswith'

I think it has something todo with nothing being passed into the lookup function, but I am really confused on how to fix this error.

Here is my code for the quote function:

@app.route("/quote", methods=["GET", "POST"])
@login_required
def quote():
"""Get stock quote."""
if request.method == "POST":
    if not request.form.get("stock"):
        return apology("please enter a stock symbol")

    #lookup stock
    result = lookup(request.form.get("quote"))

    #check if stock is a real thing
    if not result:
        return apology("the stock you choose does not exist")

    #go to see quoted results
    return render_template("postquote.html", stock=result)

else:
    return render_template("quote.html")

Here is my quote.html:

{% extends "layout.html" %}

{% block title %}
Quote
{% endblock %}

{% block main %}
    <form action="{{ url_for('quote') }}" method="post">
        <fieldset>
        <div class="text">
            <b>please enter stock's symbol:</b> 
        </div>
        <div class="form-group">
            <input autocomplete="off" autofocus class="form-control" name="stock" placeholder="stock" type="text"/>
        <div class="form-group">
            <button class="btn btn-default" type="submit">quote</button>
        </div>
    </fieldset>
</form>
{% endblock %}

And here is my lookup function in helpers.py (that I did not alter):

def lookup(symbol):
    """Look up quote for symbol."""

    # reject symbol if it starts with caret
if symbol.startswith("^"):
    return None

#reject symbol if it contains comma
if "," in symbol:
    return None

# query Yahoo for quote
# http://stackoverflow.com/a/21351911
try:
    url = "http://download.finance.yahoo.com/d/quotes.csv?f=snl1&s={}".format(symbol)
    webpage = urllib.request.urlopen(url)
    datareader = csv.reader(webpage.read().decode("utf-8").splitlines())
    row = next(datareader)
except:
    return None

# ensure stock exists
try:
    price = float(row[2])
except:
    return None

# return stock's name (as a str), price (as a float), and (uppercased) symbol (as a str)
return {
    "name": row[1],
    "price": price,
    "symbol": row[0].upper()
}

Thanks in advance for any help!

0

Sometimes all you need is another pair of eyes :) It's true, "nothing" is being passed to lookup here result = lookup(request.form.get("quote")) since there is no element with attribute name="quote" in quote.html.

1
  • Thank you so much! I think I spent too long staring at my code! ;) Jul 22 '17 at 18:23

You must log in to answer this question.

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