Sep-02-2017, 08:24 PM
Hi all,
I am new to code and follow a few tutorial in code academy and other places, and I have a question about the sequence of code.
The code shown below will print the number up to 4 (0,1,2,3,4) = 5 numbers in total. I looked at what count +=1 means it is basically count = count + 1 (adds a number every time), however I don't understand why it is put at the bottom of the code.
count=0
while(count<5):
print(count)
count +=1
else:
print("count value reached %d" %(count))
How I understand it is
count = 0 #Count starts at 1.
while (count<5): #A limit is given.
count +=1 #The computer must understand that we are adding 1 after every iteration
print (count) #We use this to print the result.
So simply my question is why count +=1 at the end?
Best Regards,
Tsvetan
I am new to code and follow a few tutorial in code academy and other places, and I have a question about the sequence of code.
The code shown below will print the number up to 4 (0,1,2,3,4) = 5 numbers in total. I looked at what count +=1 means it is basically count = count + 1 (adds a number every time), however I don't understand why it is put at the bottom of the code.
count=0
while(count<5):
print(count)
count +=1
else:
print("count value reached %d" %(count))
How I understand it is
count = 0 #Count starts at 1.
while (count<5): #A limit is given.
count +=1 #The computer must understand that we are adding 1 after every iteration
print (count) #We use this to print the result.
So simply my question is why count +=1 at the end?
Best Regards,
Tsvetan