''' Purpose: considers the subltety of swapping variable values ''' # .split() will take a string and split it by default (when there is nothing m # Why can't we just use w1, w2? # If you just do: # w1 = w2 # w2 = w1 # Remember that assignment takes the variable on the left side and assigns it the value of whatever is on the right side. # Assignment takes the value of the right hand side and makes it the new value of the left hand side. # Then what you've done is assign the value of w1 to the value of w2 and since w2 is set to the value of w1, it retains its original value since # w1 was previously assigned the value of w2! # So it's like w1 ( "wahoo" ) = w2 (" wah " ) # So now w1 = "wah" # and w2 is assigned the value of w1 ("wah") but that was the original value! Oh no! We lost the old value of w1 forever because we didn't store it somewhere. # So no switching actually takes place. # How do we fix this? What can we do? # We need a temporary variable! Why? Because we want to store the value of w1 in a variable so we don't lose it! # We want to do: # copy_w1 = w1 #sets aside a new spot in memory whre a copy of w1 can be stored # w1 = w2 # w2 = copy_w1 # We switched it successfully! # The value of w1 was stored in the copy_w1, w1 was assigned the original # value of w2, and w2 is assigned the original value of w1. # # get and echo inputs reply = input( 'Enter two words: ' ) w1, w2 = reply.split() print( ) 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 copy_w1 = w1 #sets aside a new spot in memory whre a copy of w1 can be stored w1 = w2 w2 = copy_w1 ### pause to think what has happened # print results print( 'After swapping' ) print( 'w1 =', w1 ) print( 'w2 =', w2 )