Jun-25-2020, 05:16 AM
whenever i include the average. I tried making it a int and a float. I tried using different indexes. I just can't get it to show up. But I can get it to print to the console for some reason.
One more issue with displaying API information
|
Jun-25-2020, 05:16 AM
whenever i include the average. I tried making it a int and a float. I tried using different indexes. I just can't get it to show up. But I can get it to print to the console for some reason.
What? How are you getting a value if the variable is
None ? I'm confused. Remember that we don't have the application and the state of the database, so you really need to give us more information.As an aside, I hope you learn about automated testing at some point, as having a test case that demonstrates the problem would be ideal. Oh, you possibly have scoping problems too. Consider whether your return statement is in the right place considering what your if s are checking.
Jun-25-2020, 05:43 AM
(This post was last modified: Jun-25-2020, 05:44 AM by card51shor.)
This is the code and the error i'm getting.
@app.route("/api", methods=["GET", "POST"]) @app.route("/api/<isbn>") def api(isbn=""): search = db.execute("SELECT * FROM books WHERE isbn = :isbn", {'isbn':isbn}).fetchone() if search: title = search[1] author = search[2] year = search[3] isbn: search[0] ratingNum = ratingAvg = db.execute("SELECT COUNT(rating) FROM reviews WHERE isbn = :isbn", {'isbn':isbn}).fetchone() ratingAvg = db.execute("SELECT AVG(rating) FROM reviews WHERE isbn = :isbn", {'isbn':isbn}).fetchone() if ratingNum and ratingAvg: num1 = ratingNum[0] avg1 = ratingAvg[0] avg2 = float(avg1) else: return render_template("404.html") return { "title": title, "author": author, "year": year, "isbn": isbn, "review_count": num1, "average_score": avg2 }
i honestly have no clue why i can print it to console but not on the page.
Jun-25-2020, 05:46 AM
So as I asked before, under what circumstances are you getting the
None ? What's in the database and what request are you making?
Jun-25-2020, 05:49 AM
(This post was last modified: Jun-25-2020, 06:30 AM by card51shor.)
it's only when there's no reviews when i get the error...hmm that makes sense. I should put an if statment in there. I think I got it. Let's see.
OK now my code looks like this and it shows Null for books with no reviews but now it gives me an error when it does have reviews. Here's what I'm getting. @app.route("/api", methods=["GET", "POST"]) @app.route("/api/<isbn>") def api(isbn=""): search = db.execute("SELECT * FROM books WHERE isbn = :isbn", {'isbn':isbn}).fetchone() if search: title = search[1] author = search[2] year = search[3] isbn: search[0] ratingNum = ratingAvg = db.execute("SELECT COUNT(rating) FROM reviews WHERE isbn = :isbn", {'isbn':isbn}).fetchone() ratingAvg = db.execute("SELECT AVG(rating) FROM reviews WHERE isbn = :isbn", {'isbn':isbn}).fetchone() if ratingNum and ratingAvg: num1 = ratingNum[0] avg1 = ratingAvg[0] print(avg1) else: return render_template("404.html") return { "title": title, "author": author, "year": year, "isbn": isbn, "review_count": num1, "average_score": avg1 }
Got it working here's my code: @app.route("/api", methods=["GET", "POST"]) @app.route("/api/<isbn>") def api(isbn=""): search = db.execute("SELECT * FROM books WHERE isbn = :isbn", {'isbn':isbn}).fetchone() if search: title = search[1] author = search[2] year = search[3] isbn: search[0] ratingNum = ratingAvg = db.execute("SELECT COUNT(rating) FROM reviews WHERE isbn = :isbn", {'isbn':isbn}).fetchone() ratingAvg = db.execute("SELECT AVG(rating) FROM reviews WHERE isbn = :isbn", {'isbn':isbn}).fetchone() if ratingNum and ratingAvg: num1 = ratingNum[0] avg1 = ratingAvg[0] if avg1 != None: avg2 = int(avg1) else: avg2 = 0 else: return render_template("404.html") return { "title": title, "author": author, "year": year, "isbn": isbn, "review_count": num1, "average_score": avg2 }
Jun-25-2020, 07:10 AM
I solved it guys!
|
|
Possibly Related Threads… | |||||
Thread | Author | Replies | Views | Last Post | |
Displaying selected information from XML file in the Spyder console(weather forecast) | JohnN_pl | 6 | 4,749 |
Jun-20-2020, 01:36 PM Last Post: JohnN_pl |