Definition of count += 1



I don’t think I quite understand “count +=1”. It states that it increases count by 1 on this page and the following 5 exercises makes sense with it:

However, on exercise 7, “count +=1” is used but I don’t understand the reasoning when we are not looking to have any numbers values change by a value of 1. I tried deleting “count +=1” in the code and running it and I noticed that the output displays a longer list (rather than 3) of random numbers until a 5 shows up, but that doesn’t explain to me why a “count += 1” is used in the coding. Did its meaning change? If so, how? And when do I want to use it? Here is the link to exerise 7:


It is a control on the while loop.


count is keeping track of how many guesses the user has had:

while count < 3:

see loop condition


When is count +=1 a control on the while loop vs increasing the value of output by 1?


When it reaches 4 the loop ends. That’s why it is called a control.


when its used in the loop condition. increasing count by one is not a loop control, but we do use the count variable to ensure the loop will eventually end.

but count is just a variable, we can even use it for both loop condition and printing:

import random

print "Lucky Numbers! 3 numbers will be generated."
print "If one of them is a '5', you lose!"

count = 0
while count < 3:
  num = random.randint(1, 6)
  print num
  if num == 5:
    print "Sorry, you lose!"
  count += 1
  print "you have have had: %s guesses" % count
  print "You win!"

now the variable has a double role.


I had to read this several times and compare it to the other exercises in the lesson but I think I might understand it now. Thank you.


its the code from exercise 7 with an additional print statement. Showing we can use variable for multiple purposes even within the same program. count now and informs the user of the number of guesses they have had, as well ensuring that the loop ends after the user used all his/her guesses


Yes, I saw that. I tried to play around with “count +=1”, “count +=2”, “count +=3”, “count +=10”, and “count -= 1” to get a better understanding of what count does. It makes more sense to me know.


It’s important to note that count is just a name, nothing more. The value it points to is a state, so we could more accurately call it a control state, not a control condition. Think of four hooks numbered 0, 1, 2, and 3, and count is a tag that we hang on the respective hook when we change the value. We haven’t really changed the variable, just the hook it now hangs from. When we run out of hooks, the loop ends.


I just wanted to thank you. Your explanations and metaphors help me out a lot. Even when I read through your answers for other student’s questions, I find them useful and effective. They say you are considered brilliant if you can explain complex concepts and have a 5 year old understand it. :slight_smile: Thanks for breaking it down for me and everyone else.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.