Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
More list help please!
#1
Hi everyone,

I have a question that I can't figure out, I've been trying to find the answer myself.

I have created two lists and looped through them to create the final list
fruits []

I now want to assign 5 random fruits to each 'fruit basket' that I have created

I thought this might work:

while len (fruits_1) <5

but it just keeps looping through the code over and over.

How can I give 5 different fruits to each fruit basket?
Reply
#2
(Mar-25-2021, 03:22 PM)Pytho13 Wrote: I thought this might work:

while len (fruits_1) <5

but it just keeps looping through the code over and over.

Well, the obvious question is whether or not you increase the length of friuts_1 in each iteration. Even if the results are totally wrong, your loop should stop.

Try putting this in your loop:
print(len(fruits_1)
see what shows
Reply
#3
(Mar-25-2021, 03:53 PM)michael1789 Wrote:
(Mar-25-2021, 03:22 PM)Pytho13 Wrote: I thought this might work:

while len (fruits_1) <5

but it just keeps looping through the code over and over.

Well, the obvious question is whether or not you increase the length of friuts_1 in each iteration. Even if the results are totally wrong, your loop should stop.

Try putting this in your loop:
print(len(fruits_1)
see what shows

Hi Michael,

Thanks so much for getting back to me.

I am trying to place out of 50 elements 5 in basket 1 and 5 in basket 2.

Is there a way to do that before printing?

So I now have
basket_1 = []
basket_2 = []
and I have fruits = []
fruits already contains all the fruits from two other lists so I'm wondering how to add only 5 of the 50 fruits to each basket.
I hope this makes sense.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020