Python Forum
Question about code sequence
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Question about code sequence
#1
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
Reply
#2
count +=1
increments count by 1
If you didn't have this, you would never satisfy the while condition.

you should put your print routine before the increment
Reply
#3
Thank you for that.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to reduce the following code to run in sequence? Giggel 4 2,801 Jun-28-2020, 01:31 AM
Last Post: Giggel
  Please suggest python code to format DNA sequence FASTA file rajamdade 4 3,325 Oct-24-2019, 04:36 AM
Last Post: rajamdade
  Python- Help with try: input() except ValueError: Loop code to start of sequence Aldi 2 6,555 Mar-08-2018, 03:46 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020