dimanche 21 février 2021

Building a randomized list in Python

I have a bit of an issue with my code here. I'm trying to build a program that generates a new workout routine every day. It should pull from the same list of workouts and create a new list of what the user will use that day. When I run the program, it returns a list of the same item repeated 4 times, when it should be a list of 4 unique items. I am quite new to Python and programming, but I can't seem to find the solution anywhere. I would greatly appreciate any help or advice. How can I get 4 random items from a list, append them to a new list, and have none repeating?

import random

chest_day_heavy = [["benchpress"], ["incline benchpress"], ["DB benchpress"], ["inclince DB press"],["seated machine press"]]
def workout_generator(muscle_group):
  workout_otd = []
  random_generator = random.randint(0,len(muscle_group))
  while len(workout_otd) < 4:
    workout_otd.append(muscle_group[random_generator])
    for i in range(len(workout_otd)):
      if muscle_group[random_generator] == workout_otd[i]:
        continue
  return workout_otd

print(workout_generator(chest_day_heavy))

The result, for example, I would like to be something like: [["inclince DB press"],["seated machine press"],["benchpress"],["DB benchpress"]]. No item repeated. Instead, I get something like this: [["seated machine press"],["seated machine press"],["seated machine press"],["seated machine press"]]

I may have bitten off more than I can chew after only a month of self-teaching haha




Aucun commentaire:

Enregistrer un commentaire