Learning Python, Day 11 — Loops
Yesterday, the mood was more "after telling a doctor that, no, I am not remotely trying to ever focus on my weight, thank you, I'm going to a queer karaoke night, amazing a bunch of twenty-somethings by singing to Troy Hess's "Please Don't Go Topless, Mother", and then getting maudlin drunk on plum wine cocktails, feeling old, depressed, and regretful."
I'm hoping that, today, if I just manage to get through Loops, I will have accomplished something. Ugh, save me from middle-aged ennui, Python. You're my only hope.
15 — Loops, 16 — While Loops Examples, & 17 — For Loops Examples
Back when I was dicking around in BASIC as a bored tween, I set up a program where it would ask me what I wanted to do. But it would always GOTO asking me what I wanted to do after repeating whatever I typed, so it was an endless loop that I'd eventually have to close the program to get out of.
But, hey, at least that means I know what a loop is.
Today, I'm in a The Wicker Man mood, so our Loops are going to be the ancient ritual of walking under a ring of swords while everyone yells "Chop chop chop!"
chop = 0
chopped = 5
while chop < chopped:
chop += 1
print("YOU'RE SAVED")
if chop == chopped:
print("CHOP CHOP CHOP")
>> YOU'RE SAVED
>> YOU'RE SAVED
>> YOU'RE SAVED
>> YOU'RE SAVED
>> YOU'RE SAVED
>> CHOP CHOP CHOP
I like knowing that there is a way to include a break in the loop, unlike my BASIC program.
chop = 0
chopped = 5
swords = 4
while chop < chopped:
chop += 1
print("YOU'RE SAVED")
if chop == swords:
print("CHOP CHOP CHOP")
break
print("YOU DIED")
>> YOU'RE SAVED
>> YOU'RE SAVED
>> YOU'RE SAVED
>> YOU'RE SAVED
>> CHOP CHOP CHOP
>> YOU DIED
I also like knowing there are ways to continue the program.
chop = 0
chopped = 5
swords = 4
Holly = 4
while chop < chopped:
chop += 1
print("YOU'RE SAVED")
if Holly > 2:
continue
if chop == swords:
print("CHOP CHOP CHOP")
break
print("TO THE SEA")
>> YOU'RE SAVED
>> YOU'RE SAVED
>> YOU'RE SAVED
>> YOU'RE SAVED
>> YOU'RE SAVED
>> TO THE SEA
When I learn how to have random numbers, y'all better watch out for my Chop Chop game.
I did reach a weird bit when I was trying to do a For In Loop. When I do it this way:
original_treks = range(1,7)
for i in original_treks:
print(i)
>> 1
>> 2
>> 3
>> 4
>> 5
>> 6
I get 1 to 6 listed. As it should.
So then I try to add a bit more.
original_treks = range(1,7)
for i in original_treks:
print("Star Trek ") and print(i)
>> Star Trek
>> Star Trek
>> Star Trek
>> Star Trek
>> Star Trek
>> Star Trek
This, however, just gives me Star Trek returned. No numbers.
But this
original_treks = range(1,7)
for i in original_treks:
print("Star Trek ")
print(i)
>> Star Trek
>> 1
>> Star Trek
>> 2
>> Star Trek
>> 3
>> Star Trek
>> 4
>> Star Trek
>> 5
>> Star Trek
>> 6
Gives me Star Trek and then the number on a new line.
But the and should work, right? Like, I can't have print("Star Trek " + i) because you can't concatenate a string and an integer, and, for some reason, adding in str(i) isn't turning that i into a string, so not even that works.
I'll figure it out eventually. I'm just a little annoyed.
At least the list versions work nicely.
fish = ["Coelacanth", "Sea Bass", "Dab", "Oarfish","Squid"]
for item in fish:
print(item)
>> Coelacanth
>> Sea Bass
>> Dab
>> Oarfish
>> Squid
But then I didn't pay attention to the instructor and oh no I accidentally made an infinite loop.
fish = ["Coelacanth", "Sea Bass", "Dab", "Oarfish","Squid"]
for item in fish:
print(item)
i = 0
while i < len(fish):
print(fish[i])
(It's missing the i += 1 between the while and print.)
I like coelacanths, but I didn't mean to do infinite coelacanth. Whoops.
Day 11 — Results
- While Loops are for when we don't know how many times a loop should run. So you could have infinite While Loop, like my old BASIC routine.
- To create a While Loop, add in
while [item] [condition]:. - You can break a While Loop by adding in
breakafter a condition is met. - You can also continue a While Loop by adding in
continueafter a condition is met. - For Loops (or For In Loops) are for when you have a set number of times you want the loop to run, visiting every item in a range or a list.
- To create a For In Loop, add in
for [item] in [range/list]: - You can also use
breakandcontinue. - And you can have it only go to specific items in a list by referring to its index number.
Next time, it's Functions. God, I hope this isn't BEDMAS math again.
Today's Sticker

Lord Summerisle, in front of his Wicker Man, praising the Old Ways, from the Weird Walk The Wicker Man Sticker Pack.