''' Purpose: considers the subltety of swapping variable values ''' # get and echo inputs reply = input( 'Enter two words: ' ) # Recall that .split() splits the string into a list of words separated at whitespace # s = 'I am a string' # list_words = s.split() -> ['I','am','a','string'] # w1 gets the first word in the list and w2 gets the second word in that list ^ (from the split reply) w1, w2 = reply.split() print( ) print( 'w1 =', w1 ) print( 'w2 =', w2 ) print() # swap the values of w1 and w2 # reply is 'CS 1112' # w1 is 'CS' and w2 is '1112' # CORRECT SWAPPING original_w1 = w1 # w3 is a value just to remember the value of w1 so that we don't lose it! w1 = w2 # w1 is assigned w2's value: '1112' print( w1 ) # '1112' w2 = original_w1 # w2 is assigned w1's original value that we remembered/stored in original_w1 print( w2 ) # 'CS' # We lost w1's value trying to reassign it like this: # w1 = w2 '1112' # w2 = w1 '1112' # print results # ANOTHER CORRECT SWAPPING w1, w2 = w2, w1 # The values from the left side are reassigned to the opposite values # w1 gets w2's value -> '1112' # w2 gets w1's value -> 'CS' print( 'After swapping' ) print() print( 'w1 =', w1 ) print( 'w2 =', w2 )