Robert Salter
Robert Salter

Reputation: 25

How can I condense my code to avoid repeating for loops?

I am trying to come up with a weekly meal menu, with data coming from two lists, one for meals and the other for the days of the week.

I was wondering if I could get some advice regarding the repetition of the 'for' loops, as I believe there might be a better way to do this.

"""Creating Weekly food menu by randomly selecting an item from the 
'meals' list, matching the selected item to a day-of-the-week and
printing out the weekly menu."""

import random

meals = ['Steak', 'Chicken', 'Fish', 'Hamburger', 'Taco', 'HotDog' , 
         'Bier Rocks', 'Pork']

days = ['Monday: ' , 'Tuesday: ' , 'Wednesday: ']

for num in range(1):
    meals_selected = random.choice(meals)
    print(f"The selected meal for {days[0]} {meals_selected}")

for num in range(1):
    meals_selected = random.choice(meals)
    print(f"The selected meal for {days[1]} {meals_selected}")

for num in range(1):
    meals_selected = random.choice(meals)
    print(f"The selected meal for {days[2]} {meals_selected}")

I would like to have a result similar to:

The selected meal for Monday:  Taco
The selected meal for Tuesday:  Fish
The selected meal for Wednesday:  Hamburger

Upvotes: 1

Views: 74

Answers (3)

Devesh Kumar Singh
Devesh Kumar Singh

Reputation: 20490

You can begin by using random.sample, which will give you 3 choices without replacement from the meals list in one function call.

You can also shuffle your list of meals in place via random.shuffle to induce more randomness in your menu

import random

meals = ['Steak', 'Chicken', 'Fish', 'Hamburger', 'Taco', 'HotDog' , 'Bier Rocks', 'Pork']

days = ['Monday' , 'Tuesday' , 'Wednesday']

#Shuffle the meals
random.shuffle(meals)

#Choose 3 choices
day_meals = random.sample(meals,3)

#Print day and respective Choice
for idx in range(3):
    print(f"The selected meal for {days[idx]}: {day_meals[idx]}")

The output will be

The selected meal for Monday: Steak
The selected meal for Tuesday: HotDog
The selected meal for Wednesday: Fish

The selected meal for Monday: Pork
The selected meal for Tuesday: Hamburger
The selected meal for Wednesday: Chicken

......

You can easily extend this example to include menu of all 7 days, by just taking 7 samples from random.sample

import random

meals = ['Steak', 'Chicken', 'Fish', 'Hamburger', 'Taco', 'HotDog' , 'Bier Rocks', 'Pork']

days = ['Monday' , 'Tuesday' , 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']

#Shuffle the meals
random.shuffle(meals)

#Choose 7 choices
day_meals = random.sample(meals,7)

#Print day and respective Choice
for idx in range(7):
    print(f"The selected meal for {days[idx]}: {day_meals[idx]}")

The output will be

The selected meal for Monday: Fish
The selected meal for Tuesday: Pork
The selected meal for Wednesday: Steak
The selected meal for Thursday: Bier Rocks
The selected meal for Friday: HotDog
The selected meal for Saturday: Taco
The selected meal for Sunday: Hamburger

Upvotes: 0

Pynchia
Pynchia

Reputation: 11590

In case you wanted to avoid repeating the same menu in the week:

import random

meals = ['Steak', 'Chicken', 'Fish', 'Hamburger', 'Taco', 'HotDog' , 'Bier Rocks', 'Pork']

days = ['Monday' , 'Tuesday' , 'Wednesday']

random.shuffle(meals)
for day, meal in zip (days, meals):
    print(f"The selected meal for {day}: {meal}")

Upvotes: 2

yaho cho
yaho cho

Reputation: 1779

You can loop for days.

import random

meals = ['Steak', 'Chicken', 'Fish', 'Hamburger', 'Taco', 'HotDog' , 'Bier Rocks', 'Pork']

days = ['Monday: ' , 'Tuesday: ' , 'Wednesday: ']

for day in days:
    meals_selected = random.choice(meals)
    print(f"The selected meal for {day} {meals_selected}")

Result:

The selected meal for Monday:  Chicken
The selected meal for Tuesday:  Pork
The selected meal for Wednesday:  HotDog

Upvotes: 2

Related Questions