-1

This program is a console application that displays an Application Title and the Menu Options for the user, displays a list of recipe categories (i.e. Beef, Chicken, Vegan, Side, Desert, etc.), displays a list of meals based on a user selected category, and displays meal information (Directions) based on a user selected meal. I only need help with displaying the list of meals for the user selected category.

I am using a website that provides free JSON API's for this program that all work fine. The program shows the categories and recipes but wont show the individual meals for each of the category. Its weird because I put print() statements after the if statements in the function where it gets the meals by category to try to debug it but, it prints both the pint(1) and print(2) statements so it does the getting stuff right but still also prints the else statement. This section of code is part of the recipes.py file (found under) in the search_meal_by_categories function. This is what I put in to debug it to figure out why it prints out both the if and else:

def search_meal_by_category(categories):
    lookup_category = input("Enter a category: ")
    found = False

    for i in range(len(categories)):
        category = categories[i]
        if category.get_category().lower() == lookup_category.lower():
            found = True
            print(1)

        if found:
            meals = requests.get_meals_by_category(lookup_category)
            list_meals_by_category(lookup_category, meals)
            print(2)

        else:
            print("Invalid Category, please try again")

Its very weird because it shows the recipe if you type it is but not the meals in that category. In the command menu you can: ("1 - List all Categories") ("2 - List all Meals for a Category") ("3 - Search Meal by Name") ("0 - Exit the program") Thus far, I can put in the command 1 and 3 but not 2 or 0. There are 3 different python files for this and I included each of the 3 with only that specific meals by category function from each file to make it easier (I hope?).

Basically, the goal is to ask the program to show each meal in the category you type in. Thus far, it only shows the categories and a recipes instructions if you type in the name of the recipe. However, I dint include that since its working fine.

(object.py file)

    class Category:
    def __init__(self, category):
        self.__category = category

    def get_category(self):
        return self.__category

    def set_category(self, category):
        self.__category = category

class Meal:
    def __init__(self, meal_id, meal, meal_thumb):
        self.__meal_id = meal_id
        self.__meal = meal
        self.__meal_thumb = meal_thumb

    def get_meal_id(self):
        return self.__meal_id

    def set_meal_id(self, meal_id):
        self.__meal_id = meal_id

    def get_meal(self):
        return self.__meal

    def set_meal(self, meal):
        self.__meal = meal

    def get_meal_thumb(self):
        return self.__meal_thumb

    def set_meal_thumb(self, meal_thumb):
        self.__meal_thumb = meal_thumb

(requests.py file)

    from urllib import request, parse
import json

from objects import Category, Meal


def get_categories():
    url = 'https://www.themealdb.com/api/json/v1/1/list.php?c=list'
    f = request.urlopen(url)
    categories = []

    try:
        data = json.loads(f.read().decode('utf-8'))
        for category_data in data['meals']:
            category = Category(category_data['strCategory'])

            categories.append(category)
    except (ValueError, KeyError, TypeError):
        print("JSON format error")

    return categories


def get_meals_by_category(category):
    url = 'https://www.themealdb.com/api/json/v1/1/filter.php?c=Seafood' + category
    f = request.urlopen(url)
    meals = []

    try:
        data = json.loads(f.read().decode('utf-8'))
        for meal_data in data['meals']:
            category = Meal(meal_data['idMeal'],
                            meal_data['strMeal'],
                            meal_data['strMealThumb'])

            meals.append(category)
    except (ValueError, KeyError, TypeError):
        print("JSON Format error")

    return meals
    

This is the last file of the 3 and I think where the problem lies. I put a print(1), print(2) statement after each if and it printed out both those AND the else statement? Basically, all it shows right now is "Invalid Category, please try again" like 10 times.

   import requests


def show_title():
    print("My recipes Program")
    print()


def show_menu():
    print("COMMAND MENU")
    print("1 - List all Categories")
    print("2 - List all Meals for a Category")
    print("3 - Search Meal by Name")
    print("0 - Exit the program")
    print()


def list_categories(categories):
    print("CATEGORIES")
    for i in range(len(categories)):
        category = categories[i]
        print(category.get_category())
    print()


def list_meals_by_category(category, meals):
    print(category.upper() + " MEALS ")
    for i in range(len(meals)):
        meal = meals[i]
        print(meal.get_meal())
    print()


def search_meal_by_category(categories):
    lookup_category = input("Enter a category: ")
    found = False

    for i in range(len(categories)):
        category = categories[i]
        if category.get_category().lower() == lookup_category.lower():
            found = True
            break

        if found:
            meals = requests.get_meals_by_category(lookup_category)
            list_meals_by_category(lookup_category, meals)
        else:
            print("Invalid Category, please try again")

Please comment if you need any more information, this is my first time doing a python project this big (I know this may seem very easy to everyone because everyone on here is a genius). I have genuinely been staring at this for 2 days now. Thanks everyone for any help!

quamrana
  • 27,260
  • 11
  • 50
  • 62
  • Which are the `pint(1) and print(2)` statements? – quamrana Aug 12 '20 at 19:34
  • @quamrana I added print(1) print(2) staments after the recipes. py file after the if statements in the search_meal_by_category(categories) function top see what would print out when I was debugging. – lovelylama Aug 12 '20 at 19:37
  • Sorry, I can only see one `print()` in `search_meal_by_category()`. What am I supposed to be looking for? – quamrana Aug 12 '20 at 19:40
  • @quamrana You are correct, theres only supposed to be that one statment. However, as I was debugging someone told me to add a print statement after each of the if's to see what would print. When I did, the output says: JSON Format Error & Invalid Catgory rather than showing the meals for the category I selected (I chose vegan). – lovelylama Aug 12 '20 at 19:45
  • Please update your question with your actual code. If I can't see it in the above question then it doesn't exist. – quamrana Aug 12 '20 at 19:46

1 Answers1

0

Did you mean to write this:

def search_meal_by_category(categories):
    lookup_category = input("Enter a category: ")
    found = False

    for i in range(len(categories)):
        category = categories[i]
        if category.get_category().lower() == lookup_category.lower():
            found = True
            break

    if found:
        meals = requests.get_meals_by_category(lookup_category)
        list_meals_by_category(lookup_category, meals)
    else:
        print("Invalid Category, please try again")

I have unindented the if found: part, so that when you get the found = True; break then it skips the rest of the for and detects found.

quamrana
  • 27,260
  • 11
  • 50
  • 62
  • YES! That helps so much more. However, I am getting a JSON Format Error now. Would you know how to fix that? – lovelylama Aug 12 '20 at 20:03
  • Possibly, but that is another question. Feel free to post another, but do try to keep the code volume to an absolute minimum. For this question you really only needed to post the one function. – quamrana Aug 12 '20 at 20:05