Python Forum
While statement explanation
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
While statement explanation
#2
Let's walk through the code, line by line, and keep track of what n is.

n = 3                       # 3
while n > 0:                # 3
    print(n + 1, end=' ')   # 3 (print 4)
    n -= 1                  # 2

while n > 0:                # 2
    print(n + 1, end=' ')   # 2 (print 3)
    n -= 1                  # 1

while n > 0:                # 1
    print(n + 1, end=' ')   # 1 (print 2)
    n -= 1                  # 0

else:
    print(n)                # 0
Reply


Messages In This Thread
While statement explanation - by alkhufu2 - Aug-31-2020, 11:45 PM
RE: While statement explanation - by nilamo - Aug-31-2020, 11:58 PM
RE: While statement explanation - by deanhystad - Sep-01-2020, 03:44 PM
RE: While statement explanation - by alkhufu2 - Sep-02-2020, 05:46 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  New learner in Python, and need help on the term explanation BaicaiPy 3 1,338 Oct-15-2022, 03:31 PM
Last Post: Yoriz
  Some line code explanation Chrilo06 3 2,188 Feb-24-2022, 06:24 PM
Last Post: deanhystad
  XOR solution explanation needed. omm 7 3,311 Oct-26-2020, 06:30 AM
Last Post: omm
  Output explanation AmanTripathi 2 2,883 Feb-14-2018, 03:03 PM
Last Post: AmanTripathi
  need help with some explanation vincelim99 2 3,682 Mar-24-2017, 04:12 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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