mardi 15 juin 2021

Flask If Statement - Range for list index

customer_data.json (loaded as customer_data)

{
  "customers": [
    {
      "username": "anonymous",
      "id": "1234",
      "password": "12341234",
      "email": "1234@gmail.com",
      "status": false,
      "books": [
        "Things Fall Apart",
        "Fairy Tales",
        "Divine Comedy"
      ]
    }
  ]
}

Example post in new_catalog.json. (loaded as posts)

{
  "books": [
    {
      "author": "Chinua Achebe",
      "country": "Nigeria",
      "language": "English",
      "link": "https://en.wikipedia.org/wiki/Things_Fall_Apart\n",
      "pages": 209,
      "title": "Things Fall Apart",
      "year": 1958,
      "hold": false
    }
}

Necessary code in Flask_practice.py

    for customer in customer_data['customers']:
        if len(customer['books']) > 0:
            for book in customer['books']:
                holds.append(book)
    
    for post in posts['books']:
        if post['title'] == holds[range(len(holds))]:
            matching_posts['books'].append(post)

holds[range(len(holds))] doesn't work.

I am trying to go through each of the books in holds using holds[0], holds[1] etc and test to see if the title is equal to a book title in new_catalog.json.

I'm still new to Flask, Stack Overflow and coding in general so there may be a really simple solution to this problem.

Aucun commentaire:

Enregistrer un commentaire