Discover how to efficiently use Python loops and conditional statements to categorize fruits into tasty treats. Gain practical insights into handling strings and conditions to streamline your Python coding skills.
Key Insights
- Use Python's
len()
function and loops to conditionally create lists, such as making jelly beans only from fruits with names of five or fewer letters (e.g., kiwi, apple, mango). - Understand the proper use of
if
,elif
, andelse
statements to create different outcomes based on specific conditions, such as making jelly for fruits exactly five letters long (like grape), popsicles for fruits starting with "P," jam for fruits ending with "Y," and lollipops for all other cases. - Carefully manage the indentation within loops and conditionals to ensure Python code runs correctly and as intended, with particular attention to nesting and order of condition checks.
Note: These materials offer prospective students a preview of how our classes are structured. Students enrolled in this course will receive access to the full set of materials, including video lectures, project-based assignments, and instructor feedback.
This is a lesson preview only. For the full lesson, purchase the course here.
Challenge: make jelly beans again, but this time only if the fruit is five letters or less. Save the result to j_beans_five_char_max
. All right, we're gonna save the result to a new list.
j_beans_five_char_max
will be our list name. So this would only contain fruit jelly beans made from fruits that are five letters or less. So kiwi, lime, lemon, apple, mango, grape—good; cherry, banana, watermelon—not good (ineligible).
We have to do that in a loop. Using a loop, make jelly beans again, but this time only if the fruit is five letters or less. How do you check? Hint: len(list)
returns the number of items in a list.
And len(string)
returns the number of characters in a string, which is what you want to do, right? You have to check the length—how many characters are in each fruit. So pause, try it, and come back for the solution. Okay, here we go.
We're gonna say: for fruit, we might make jelly beans, we might not. We have an if condition. We're going to say: if len(fruit)
is less than or equal to 5, that would be it—like mango, lime.
Then we're going to take our little kind of awkwardly named list and append the current fruit as a jelly bean. And then when we're all done—when the loop ends—notice we outdent all the way out. We're not in the loop anymore when we print.
If you print in the loop, you'll be printing every single time, which you don't want to do. Every time the loop runs, you just want to accumulate and then print once when you're all done.
And there it is. And we could pre-print this, right? Yep, there they are. Challenge: make jelly beans if the fruit is five letters or less.
Okay, in other words, carrying on with this idea, but do an else part: else, make lollipops. So every single fruit is either going to get made into a jelly bean or a lollipop.
And then save all to a new list called treats
. So pause, try it. It's basically continuing with the if—you have to add the else part now.
Remember, else doesn't take a condition. So it's just else:
, do that. Okay, pause for those and come back when you're ready for the solution.
Okay, here is the solution to the number 15 step challenge. We're gonna say treats = []
—a new empty list, right? It says save all to treats
list, which we have to make. Then we're gonna loop: for fruit in fruits:
If we could copy-paste from above, but better practice is to type it out. If the current fruit is five letters or less (this I’ll copy), we're gonna make a jelly bean, but we're gonna put it in treats
this time.
Else, we're gonna append a lollipop. And then when we're all done, we'll print treats
. Kaboom—jelly beans and lollipops.
Rolling another challenge: Fruit Loop Challenge. If the fruit is less than five letters, make a jelly bean. Else if the fruit has exactly five letters, make jelly. Else if the fruit starts with a P, make a popsicle. Else if the fruit ends with a Y, make jam.
Hint: string characters have index positions as we know. Index 0 is the first character, index -1 is the last character. Else, if all else is false, make a lollipop.
And we're gonna use treats
again. So you're making a jelly bean, jelly, popsicle, jam, or a lollipop. Try it out.
if
, elif
, elif
, elif
, else
. Go look at lesson two to refresh on the syntax with the elif
. In other words, between the if
and the else
—like if and else are the bread—you have a sandwich with three elif
s, each with its own condition, with else never having a condition.
Pause, give it a try, come back when you're ready for the solution. All right, we're ready to try the solution here. We're gonna say for fru in fruits:
(and we need treats = []
as the new empty list to start). If len(fru) < 5
, then we're gonna say treats.append(fru + " jelly bean")
.
elif len(fru) == 5:
then we will append(fru + " jelly")
—make apple jelly, right? Grape jelly. elif fru[0] == "P":
that would only be true if it starts with a P. Index 0 of the string. Then you're going to make a popsicle.
Starts with P—make a popsicle. So you'll have papaya popsicle. If the fruit ends with Y—like cherry, berry, strawberry, any of the berries—we're gonna make jam. Raspberry jam. And else
we're gonna do a lollipop. Mmm, jam. And then we will print what we got.
So that's the solution right there. Looping, if
, and you've gotta get your indentations correct. The if/elif
block is indented in the loop; it's running once every time the loop runs, so anything that's running again and again inside the loop has to be in the loop.
And then the thing you're running if the if
is true has to be indented. Run—boom, there it is, it works. Jelly, lollipop, jam, jam, jam, jam.
Yeah, all the berries are jams. Grape jelly—because it's five letters. Apple jelly. Lollipop—none of the above.
Kiwi jelly bean—less than five. Lime jelly bean—those are the only two jelly beans. Peach? No, excuse me—papaya and pineapple are popsicles.
How come peach is not a popsicle? Because peach got picked off by the jelly, right? That check on five letters happened before the check on P. So that is why your peach is jelly and not a popsicle. Up next.