Sure, let's learn about Python loops and statements with some fun examples!
While Loop:
Imagine you're playing a game where you have to collect stars. You keep collecting stars until you reach 10.
stars_collected = 0
while stars_collected < 10:
print("Collecting a star!")
stars_collected += 1
In this code, you collect a star (stars_collected += 1) and the game keeps going (while stars_collected < 10) until you have 10 stars.
Break Statement:
Now, what if you find a magic star that lets you skip to the end of the game? You can use the break statement to stop the loop early.
stars_collected = 0
while stars_collected < 10:
print("Collecting a star!")
if stars_collected == 5:
print("Found a magic star! Skipping to the end.")
break
stars_collected += 1
Here, when you find the magic star (stars_collected == 5), you use break to stop collecting stars and skip to the end of the game.
Continue Statement:
What if you want to skip collecting a star when it's cloudy? You can use the continue statement to skip to the next round of collecting.
stars_collected = 0
while stars_collected < 10:
print("Collecting a star!")
if stars_collected == 5:
print("It's cloudy! Skipping this round.")
continue
stars_collected += 1
In this code, when it's cloudy (stars_collected == 5), you use continue to skip collecting a star and go back to the start of the loop.
Else Statement:
Finally, what if you want to do something special after collecting all the stars? You can use the else statement with a loop.
stars_collected = 0
while stars_collected < 10:
print("Collecting a star!")
stars_collected += 1
else:
print("All stars collected! Time for a party!")
Here, after collecting all the stars (stars_collected >= 10), the else statement is executed, and you throw a party!
Remember, loops are like playing games where you keep doing something until a condition is met. The break statement is like finding a shortcut, continue is like skipping a turn, and else is like celebrating after finishing the game! 🌟🎉🎈
Sure, let's learn about Python loops and statements with some fun examples!
While Loop: Imagine you're playing a game where you have to collect stars. You keep collecting stars until you reach 10.
In this code, you collect a star (
stars_collected += 1
) and the game keeps going (while stars_collected < 10
) until you have 10 stars.Break Statement: Now, what if you find a magic star that lets you skip to the end of the game? You can use the
break
statement to stop the loop early.Here, when you find the magic star (
stars_collected == 5
), you usebreak
to stop collecting stars and skip to the end of the game.Continue Statement: What if you want to skip collecting a star when it's cloudy? You can use the
continue
statement to skip to the next round of collecting.In this code, when it's cloudy (
stars_collected == 5
), you usecontinue
to skip collecting a star and go back to the start of the loop.Else Statement: Finally, what if you want to do something special after collecting all the stars? You can use the
else
statement with a loop.Here, after collecting all the stars (
stars_collected >= 10
), theelse
statement is executed, and you throw a party!Remember, loops are like playing games where you keep doing something until a condition is met. The
break
statement is like finding a shortcut,continue
is like skipping a turn, andelse
is like celebrating after finishing the game! 🌟🎉🎈