Mar-30-2019, 08:14 PM
(This post was last modified: Mar-30-2019, 08:14 PM by juan_de_comala.)
Hi all,
So I'm trying to understand while loops. I'm stumped why this loop executes 8 times, when I'm trying to have it execute no more than 6 times. Could someone please explain? (Homework question, I've modified the variable names and contents from the original.)
1 :
Today I don't feel like doing anything
2 :
I just wanna lay in my bed
3 :
Don't feel like picking up my phone
4 :
So leave a message at the tone
5 :
Today I don't feel like doing anything
6 :
I just wanna lay in my bed
7 :
Don't feel like picking up my phone
8 :
So leave a message at the tone
So I'm trying to understand while loops. I'm stumped why this loop executes 8 times, when I'm trying to have it execute no more than 6 times. Could someone please explain? (Homework question, I've modified the variable names and contents from the original.)
lyrics = ["Today I don't feel like doing anything", "I just wanna lay in my bed", "Don't feel like picking up my phone", "So leave a message at the tone"] repeat_number = 6 repeat = 0 while repeat < repeat_number: for lyric in range(0,(len(lyrics))): repeat += 1 print(repeat, ':') print(lyrics[lyric])Here are the results -- as you can see, the loop continues executing twice more, then though repeat > repeat_number:
1 :
Today I don't feel like doing anything
2 :
I just wanna lay in my bed
3 :
Don't feel like picking up my phone
4 :
So leave a message at the tone
5 :
Today I don't feel like doing anything
6 :
I just wanna lay in my bed
7 :
Don't feel like picking up my phone
8 :
So leave a message at the tone