Home > Software design >  trying to make a random meal for the week
trying to make a random meal for the week

Time:11-08

i am trying to have a code to give me a list of meals and put the day i will be having this. but when i run it it put its out the same item for the week and repeats that with the next meal. not sure how to fix this.

monday: random meal
tuesday: random meal
wednesday: random meal
thursday: random meal
friday: random meal
saturday: random meal
sunday: random meal
import pandas as pd
import random


def main():
    # I want to do for this code is to random meals per week and put in my done Excel file to go shopping.
    # grab list of meals to make.
    db = pd.read_excel("meals.xlsx")
    week_days = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]

    for i in db.index:
        random_item = [db["meals"].loc[i]]
        random_item = random.choice(random_item)
        for i in week_days:
            print(f"{i}: {random_item}")


if __name__ == "__main__":
    main()

but it double and if i add more to the list it just adds more to the list.

monday: Chicken & Veggie Stir-fry
tuesday: Chicken & Veggie Stir-fry
wednesday: Chicken & Veggie Stir-fry
thursday: Chicken & Veggie Stir-fry
friday: Chicken & Veggie Stir-fry
saturday: Chicken & Veggie Stir-fry
sunday: Chicken & Veggie Stir-fry
monday: Easy Butter Chicken
tuesday: Easy Butter Chicken
wednesday: Easy Butter Chicken
thursday: Easy Butter Chicken
friday: Easy Butter Chicken
saturday: Easy Butter Chicken
sunday: Easy Butter Chicken

not really sure know how to fix this.

CodePudding user response:

In the following part, you are basically assigning the same thing [random_item] over and over for all week days:

for i in db.index:
        random_item = [db["meals"].loc[i]]
        random_item = random.choice(random_item)
        for i in week_days:
            print(f"{i}: {random_item}")  #here is the logically wrong part

First, just change the code so that you will get a random_item, then assign it for the first day of the week. Then the next iteration should be on the next day of the week, and so on.

So, your first loop should be on the week days and not in the db.index.

CodePudding user response:

import pandas as pd
import random


def main():
    # I want to do for this code is to random meals per week and put in my done Excel file to go shopping.
    # grab list of meals to make.

    db = pd.read_excel("meals.xlsx")
    week_days = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]

    for i in week_days:
        df_percent = db.sample(frac=0.7)
        df_rest = db["meals"].loc[~db.index.isin(df_percent.index)]
        df_rest = df_rest.values
        print(f"{i}: {df_rest[0]}")

if __name__ == "__main__":
    main()

this fixed my issue. thanks guys!

monday: Easy Butter Chicken
tuesday: Easy Butter Chicken
wednesday: Easy Butter Chicken
thursday: Chicken & Veggie Stir-fry
friday: Chicken & Veggie Stir-fry
saturday: Chicken & Veggie Stir-fry
sunday: Easy Butter Chicken
  • Related