So I made this program, where you can add a recipe which is saved as a json and you can get a plan for your meals and a grocerylist back.Program that help you to decide what to eat for the week   You should start adding some Recipies and ingredience you need   for the Recipie. If you have at least 7 of them they will be   used to tell you what you could cook and give you a grocery list   Keywords are add_recipe and get_mealsimport sysimport getoptimport jsonimport globimport randomclass Usage(Exception):    def __init__(self, msg):        self.msg = msgdef main(argv=None):    Takes the Keywords add_recipe and get_meals and    calles the underlying functions.        if argv is None:        argv = sys.argv    try:        try:            opts, args = getopt.getopt(argv[1:], hamg, [help, add                                                         get])        except getopt.error as msg:            raise Usage(msg)        for o, args in opts:            print(args)            if o in (-h, --help):                print('''Valid options ase -a to add a recipe and -m                      to get meals and -g to get meals and grocerielist                      ''')                return 1            elif o in (-a):                save_recipe(add_recipe())            elif o in (-m):                recipes = get_recipes()                weekly_meals = return_shuffled_max_seven(recipes)                print_meals(weekly_meals)            elif o in (-g):                recipes = get_recipes()                weekly_meals = return_shuffled_max_seven(recipes)                print_meals(weekly_meals)                print_grocerylist(weekly_meals)            else:                raise Usage(No valid option)    except Usage as err:        print(sys.stderr, err.msg)        print(sys.stderr, for help use --help)        return 2def add_recipe():     Asks the user interactivly to input a recipe name    and the ingredience    Returns    -------        A dictionary with the recipe name and the list of ingedience        Example        -------            {'Applepie':['Apple','Pie']}        print(Name of the recipe\n)    name = input(> )    recipe = []    while True:        print(Add an ingredience or finalize the recipe with 'q')        ingredience = input(> )        if ingredience in 'q':            break        else:            recipe.append(ingredience)    return {name: recipe}def save_recipe(recipe):    Saves the recipe to a json file    #Get the first key from the dictionary: Name of the recipe    name = next(iter(recipe.keys()))    with open(name+'.recipe', 'w') as f:        json.dump(recipe, f, indent=1)def get_recipes():    Gets all recipes and returns them as one list    grand_list_of_recipes = []    for recipe in glob.glob(*.recipe):        with open(recipe, 'r') as f:            grand_list_of_recipes.append(json.load(f))    return grand_list_of_recipesdef return_shuffled_max_seven(a_list):     Returns 7 items from a list    random.shuffle(a_list)    return a_list[:7]def print_meals(weekly_meals):    Prints a weekplan for meals    week=[]    for meal in weekly_meals:        name = next(iter(meal.keys()))        week.append(name)    if len(week) < 7:        print(You only have +str(len(week))+ recipes)        for i in range(7-len(week)):            week.append('')    print('Mondays:')    print('    '+str(week[0])+'\n')    print('Tuesdays:')    print('    '+str(week[1])+'\n')    print('Wednesday:')    print('    '+str(week[2])+'\n')    print('Thursday:')    print('    '+str(week[3])+'\n')    print('Friday:')    print('    '+str(week[4])+'\n')    print('Saturday:')    print('    '+str(week[5])+'\n')    print('Sunday:')    print('    '+str(week[6])+'\n')def print_grocerylist(weekly_meals):    Prints the grocersylist    meals=[]    for meal in weekly_meals:        name = next(iter(meal.keys()))        meals.append(name)    recipe_list=[]    for idx, meal  in enumerate(meals):        recipe_list.append(weekly_meals[idx][meal])    grocery_list=[]    for sublists in recipe_list:        grocery_list += sublists    print(Grocery List:\n)    grocery_list = list(set(grocery_list))    for item in grocery_list:        print(    * +item+\n)if __name__ == '__main__':    sys.exit(main())
Meal plan for the week
python;beginner
Your imports should, per the style guide, be in alphabetical order:import getoptimport globimport jsonimport randomimport sysUnless you have a particular reason/requirement for using getopt, you should strongly consider using argparse instead.There doesn't seem to be much point to defining your own error, catching getopt.error and raising Usage - why not just omit the handling and allow the error from getopt to filter back up?Rather than have a separate .recipe file for each recipe, why not keep them all in a master dictionary all_recipes = {name: recipe, ...} and json.dump that whole dictionary into a single file? You already return a dictionary from add_recipe, so you could:all_recipes.update(add_recipe())Rather than fix the name e.g.def return_shuffled_max_seven(a_list)make the number a parameter:def return_shuffled(a_list, max_=7):You could then call return_shuffled(some_list, None) to get all of the contents back. Also, note that it shuffles the argument list in-place then returns a new slice from that list - this is unconventional behaviour and should be clearly documented (indeed, more docstrings throughout would be helpful).You have a lot of repetition on days of the week in print_meals, consider a simplification starting with:for index, day in enumerate(['Monday', 'Tuesday', ...]):Rather than:grocery_list=[]for sublists in recipe_list:    grocery_list += sublistsprint(Grocery List:\n)grocery_list = list(set(grocery_list))for item in grocery_list:    print(    * +item+\n)Make grocery_list a set from the start:grocery_list = set()for ingredients in recipe_list:    grocery_list.update(ingredients)print(Grocery List:\n)for ingredient in grocery_list:    print(    * {}\n.format(ingredient))Note also use of str.format instead of concatenation.