0

Hi I am new with Python and I can't seem to find the solution to this. I am using python with Flask to create a simple api to post and get products with name, price and isbn. On on the post method it gives me an issue on line 48 near " books.insert(0, new_book)". The error is: IndentationError: unindent does not match any outer indentation level

from flask import Flask, jsonify, request 

app = Flask(__name__)

books =[
    {   
        'name': 'Bill Gates',
        'price': 8.99,
        'isbn': 6546984984965161
    },
    {
        'name': 'Steve Jobs',
        'price': 6.99,
        'isbn': 651468498494698
    }
]

#Get /books/6546984984965161

# post books 
#{
#       "name": "Ana Banana",
#       "price": 6.99,
#       "isbn": 651468498494698
#}

def valid_book_object(book):
    if "isbn" in book and "name" in book and "price" in book:
        return True
    else:
        return False


@app.route('/books', methods=['GET', 'POST'])
def add_book():
    # If request is GET, just return JSON data of books.
    if request.method == 'GET':
        return jsonify({'books': books})
    else:
        # This is part if it is POST request
        request_data = request.get_json()
        if valid_book_object(request_data):
            new_book = {
                "name": request_data['name'],
                "price": request_data['price'],
                "isbn": request_data['isbn']
            }
            books.insert(0, new_book)
            return "True"
        else:
            return "False"


# GET /books/456
@app.route('/books/<int:isbn>')  # second endpoint
def get_book_by_isbn(isbn):
    return_value = {}
    for book in books:
        if book["isbn"] == isbn:
            return_value = {
                'name': book["name"],
                'price': book["price"]
            }
            return jsonify(return_value)
    return 'No book with {} isbn value'.format(isbn)


if __name__ == '__main__':
    app.run(port=5000)enter code here
  • 1
    Does this answer your question? [IndentationError: unindent does not match any outer indentation level](https://stackoverflow.com/questions/492387/indentationerror-unindent-does-not-match-any-outer-indentation-level) – 0x5453 May 22 '20 at 17:05
  • yes I've set Set Sublime Text to use tabs for indentation: View --> Indentation --> Convert Indentation to Tabs thanks – littlemisscomputerscientist May 22 '20 at 17:09

1 Answers1

1

Set Sublime Text to use tabs for indentation: View --> Indentation --> Convert Indentation to Tabs