簡體   English   中英

Python 和 Flask: IndentationError: unindent 不匹配任何外部縮進級別

[英]Python and Flask: IndentationError: unindent does not match any outer indentation level

嗨,我是 Python 的新手,我似乎找不到解決方案。 我正在使用 python 和 Flask 創建一個簡單的 api 來發布和獲取帶有名稱、價格和 isbn 的產品。 在 post 方法上,它在“books.insert(0, new_book)”附近的第 48 行給了我一個問題。 錯誤是: IndentationError: unindent does not match any external 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

將 Sublime Text 設置為使用制表符進行縮進:查看 --> 縮進 --> 將縮進轉換為制表符

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM