''' Purpose: considers the subltety of swapping variable values ''' # get and echo inputs reply = input( 'Enter two words: ' ) # remember split() is a function that strings have that # splits a string based on its white space w1, w2 = reply.split() print( 'Before swapping' ) print( 'w1 =', w1 ) print( 'w2 =', w2 ) ### pause to think what has happened print(); input( 'Enter when ready: ' ); print() # swap the values of w1 and w2 # What did everyone try? # What did not work? #w1 = w2 #w2 = w1 # Why? w1 has been changed to w2 so its old value is gone # What worked???? #remember_original_w1 = w1 #w1 = w2 #w2 = remember_original_w1 # or #x1 = w1 #x2 = w2 #w1 = x2 #w2 = x1 w1, w2 = w2, w1 # ^^^ What? This will only work in python # In python this means take the first value before the equals # and save the first val after the equals to it; # then do the same for the second values; this is called multi assignment # w2, w1 = reply.split() # ( Note this will also work ) ### pause to think what has happened # print results print( 'After swapping' ) print( 'w1 =', w1 ) print( 'w2 =', w2 )