''' Purpose: counting backward using using loops ''' n = 10 # way 1: # range() function has a third argument called the step variable # it tells the range how many steps to take # ex: range( 0, 10, 2 ) --> only get every other number: 0, 2, 4, 6, 8 # range() also allows you to put the bigger number as the first number and the smaller number as the second number # but you must also put the step argument to tell the range() to go backward # otherwise, there are no numbers that include the bigger number and go UP to but not including the smaller # second number # ex: range( 10, 0, -1 ) --> numbers will start from 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 (doesn't include the # second number - always 1 more than the second number) for i in range( n, 5, -1 ): print( i ) print() # way 2 # do a subtraction accumulation with a while loop or a for loop # accumulate a difference difference = n print( difference ) # for n times, each time subtract one from the difference accumulator for i in range( 0, n ): difference = difference - 1 print(difference) print() # way 3: use a while loop # very similar to the for loop in way 2 # TRY IT OUT! # way 4: for i in range( 0, n + 1 ): down = n - i # i updates from 0 to include n # i = 0 # down = n - 0 --> n # if n = 10 --> down = 10 # i = 1 # down = n - 1 # if n = 10 --> down = 9 # i = 2 # down = n - 2 # if n = 10 --> down = 8 # ... # i = n # down = n - n # if n = 10 --> down = 0 print( down ) print() # way 5 for i in reversed( range( 0, n + 1 ) ): # get the reverse of the range 0 to n print( i ) print()