''' Purpose(s): String chrestomathics Motivate the need for looping ''' ADAGE = "Love is all you need" # prompt and get from user the number of occurrences to be printed reply = input( "Enter number of lines to be printed: " ) n = int( reply ) # since we want an integer, reply is a string, so we cast it to an int # print the n occurrences of the string of interest # sometimes there are too many print statements for us to do manually --> need loop # DO NOT USE for, in AS VARIABLE NAMES !!!!!!!! # keywords are reserved !!!! DON'T USE THEM AS VARIABLES # if you reassign them, it will remove all the function from it # for --> python recognizes that you want to start a for loop # expects a loop variable # you do NOT need to define this before the loop # even if you did, the for loop will assign it the values inside the sequence of values # can be named anything # in --> expects a sequence of values; separate the loop variable from the sequence # expects sequence of values like range(x, y) # : # counting loop - repeat something a certain number of time # range(x, y) # x = start # y = up to but not including y - stop point # includes y - 1 !!!!!! for loop_variable in range(0, n): # loop_variable automatically takes on the values 0, 1, 2, 3, ... n - 1 one at a time # indent the body of the loop - tells python these codes belong within the loop # body = action of the loop # here we say we start at 0 # and we go all the way up to n - 1 !!!!! # between 0 and n-1, there are n numbers !!!!!!! print( loop_variable, ': ', ADAGE ) # this print() is part of the loop, so we do this n times # each time through the loop, we print the above things n times! # loop_variable goes through each number within the range we defined in the range() function # 1st time: loop_variable = 0 # 2nd time: loop_variable = 1 # ... # n_th time: loop_variable = n - 1 # yes, loop_variable automatically updates every time the for loop restarts # loop_variable could be named anything that makes sense in context # you don't need to define the loop_variable before the for loop !!!!!!!!! # generally don't use loop_variable outside the loop, otherwise, you might override something # helps you with understanding your code better and help you better organize your code print() # this is part of the loop, along with the above print statement; # this will print an empty line underneath each line of the above print statement # so this code will print loop_variable: ADAGE then print an empty line underneath it # there are technically no max # of actions under loops but generally don't want it too big print() # not indented, so not part of loop --> we only do this once total = 0 # accumulator - we want to accumulate the values between the values of 0 and n for i in range(0, n): print( ADAGE ) total = total + i # each time through the loop, we use i and adding to total --> we keep updating total each # time through the loop # before loop runs, total = 0 # 1st time: total = total + 0 --> 0 + 0 => total = 0 # 2nd time: total = total + 1 --> 0 + 1 => total = 1 # 3rd time: total = total + 2 --> 1 + 2 => total = 3 # nth time: total = total + (n-1) --> __ + ( n - 1 ) = total = whatever the sum is # don't need to use the loop variable inside the loop, but you NEED one when you start a for loop # printing total inside the for loop will give you the progress of the updates print( 'total =', total ) # when we print the value in total after the for loop, we only print the very last thing that was updated to total print() # if n = 3, the final value of i = 2 (n - 1), so after the above for loop (line 67), i = 2 here print( i ) print() # THIS IS NOT THE SAME AS RANGE !!!!! # i will just be the values inside () # i = 0 # i = n # NOT A RANGE !! MUST USE range(0, n) # this loop will only run 2 times, because there are only 2 values inside () for i in (0, n): print( i, ': ', ADAGE ) # range(0, n) == range( 0, n ) == range(0,n) --> whitespaces don't matter * this is a style rule, not a syntax rule # read for loops mindfully! recognize what you have written, not what you meant when debugging # use print statements to help you while debugging: check out your variables, check for loops, ... print() # i still exists after for i loop -- IT CONTAINS THE VERY LAST VALUE THAT WAS ASSIGNED TO IT # here the last value of i is from the for loop at line 84 = n --> if n = 3, then i = 3 print( i )