Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Could you please look at the sandbox function (in bold and italic) and explain e

ID: 3859258 • Letter: C

Question

Could you please look at the sandbox function (in bold and italic) and explain every line in the code and why the statement is written. I have a job interview and I need to explain this. I do not know Python. I know Java

import os
import inspect
import sys


def main():
    sandbox()

    def game_over():
        print("I sense a disturbance in the code")
        os._exit(1)
        print('The code is strong in this one')

    def win():
        # TODO fail_if_not_called_from_line_8()
        print(FROGSLAYER_LOGO)

    game_over()
    win()

def sandbox():
    #####################################################
    # Begin sandbox
    # you can only code in this sandbox
    #####################################################
    global print
    old_print = print

    def new_print(*args, **kwargs):
        pass

    print = new_print

    def patch():
        global print
        print = old_print

    def exit(code):
        patch()

    os._exit = exit
    #####################################################
    # End sandbox
    #####################################################


FROGSLAYER_LOGO = """
                                                    `.-            -.`                                       
                                            `.:+sydmmNd`          `dNmmdys+:.                                
                                        ./ohmmNNNNNNNNd`          `dNNNNNNNNmmho/.                           
                                    ./sdmNNNNNNNNNNNNNd`          `dNNNNNNNNNNNNNmds/.                       
                                 .+hmmNNNNNNNNNNNNNNNNd`          `dNNNNNNNNNNNNNNNNmmh+.                    
                              `/ymNNNNNNNNNNNNNNNNNNNNd`          `dNNNNNNNNNNNNNNNNNNNNmy/`                 
                            .odmNNNNNNNNNNNNNNNNNNmdhyo            oyhdmNNNNNNNNNNNNNNNNNNmdo.               
                          -smNNNNNNNNNNNNNNNNmhs+:.```              ```.:+shmNNNNNNNNNNNNNNNNms-             
                        .smNNNNNNNNNNNNNNmho:.`                            `.:ohmNNNNNNNNNNNNNNms.           
                     `+dNNNNNNNNNNNNNmh+-`                                    `-+hmNNNNNNNNNNNNNd+`         
                     -hNNNNNNNNNNNNNds-`                                          `-sdNNNNNNNNNNNNNh-        
                   `+mNNNNNNNNNNNNdo.               .-:-.`                           .odNNNNNNNNNNNNm+`      
                  `smNNNNNNNNNNNdo.               -sdmNmdh/`                           .odNNNNNNNNNNNms`     
                 .yNNNNNNNNNNNmy.                -mNNNNNNNm+                             .ymNNNNNNNNNNNy.    
                `hNNNNNNNNNNNm/`                 +NNNNNNNNNh                              `/mNNNNNNNNNNNh`   
               `yNNNNNNNNNNNh-                   `ymNNNNNNm:                                -hNNNNNNNNNNNy`  
               oNNNNNNNNNNNh.                     `/hNNNNNs          `.--.`                  .hNNNNNNNNNNNo  
              :mNNNNNNNNNNh.                        /NNNNNs        `+hmNNmh+`                 .hNNNNNNNNNNm:
             `hNNNNNNNNNNm-                         .mNNNNs        sNNNNNNNNo                  -mNNNNNNNNNNh`
             /NNNNNNNNNNN/                           hNNNNh       `mNNNNNNNNd`                  /NNNNNNNNNNN/
             dNNNNNNNNNNh`          .-/:.`           oNNNNm`       sNNNNNNNmo                   `hNNNNNNNNNNd
            -NNNNNNNNNNN/         .smNNNNd/          /NNNNN.       sNNNNNmy:                     /NNNNNNNNNNN-
            .///////////`         hNNNNNNNm/         -NNNNN+      `mNNNNy`                       `///////////.
                                  dNNNNNNNNh`        .mNNNNy      +NNNNm.                                    
                                  :dNNNNNNNNh-       `dNNNNm.    .mNNNNo                                     
                                   `/ossdNNNNm+`     `dNNNNNo   `hNNNNm.                                     
                                        `+mNNNNd/`   :NNNNNNNs/+dNNNNNs                                      
                                          .sNNNNNdo/+mNNNNNNNNNNNNNNNN/                                      
            .///////////`                   /dNNNNNNNNNNNNNNNNNNNNNNNN/                          `///////////.
            -NNNNNNNNNNN/                    .yNNNNNNNNNNNNNNNNNNNNNNNh`         .oyhs/`         /NNNNNNNNNNN-
             dNNNNNNNNNNh`                    `omNNNNNNNNNNNNNNNNNNNNNNy:.```..:omNNNNms        `hNNNNNNNNNNd
             /NNNNNNNNNNN/                      +mNNNNNNNNNNNNNNNNNNNNNNmmdddmmmNNNNNNNm        /NNNNNNNNNNN/
             `hNNNNNNNNNNm-                      sNNNNNNNNNNNNNNNNNNNNNNNNNNNNNmdhdNNNh:       -mNNNNNNNNNNh`
              :mNNNNNNNNNNh.                     .mNNNNNNNNNNNNNNNNNNNNNNNNmho:.``.---`       .hNNNNNNNNNNm:
               oNNNNNNNNNNNh.                     +NNNNNNNNNNNNNNNNNNNNNNmo.`                .hNNNNNNNNNNNo  
               `yNNNNNNNNNNNh-                    `yNNNNNNNNNNNNNNNNNNNms-                  -hNNNNNNNNNNNy`  
                `hNNNNNNNNNNNm/`                   `sNNNNNNNNNNNNNNNNNd:`                 `/mNNNNNNNNNNNh`   
                 .yNNNNNNNNNNNmy.                   `+dNNNNNNNNNNNNNd+.                  .ymNNNNNNNNNNNy.    
                  `smNNNNNNNNNNNdo.                   `/ydmmNNNmdhs:.                  .odNNNNNNNNNNNms`     
                   `+mNNNNNNNNNNNNdo.                    `.-::--.`                   .odNNNNNNNNNNNNm+`      
                     -hNNNNNNNNNNNNNds-`                                          `-sdNNNNNNNNNNNNNh-        
                      `+dNNNNNNNNNNNNNmh+-`                                    `-+hmNNNNNNNNNNNNNd+`         
                        .smNNNNNNNNNNNNNNmho:.`                            `.:ohmNNNNNNNNNNNNNNms.           
                          -smNNNNNNNNNNNNNNNNmhs+:.```              ```.:+shmNNNNNNNNNNNNNNNNms-             
                            .odmNNNNNNNNNNNNNNNNNNmdhyo            oyhdmNNNNNNNNNNNNNNNNNNmdo.               
                              `/ymNNNNNNNNNNNNNNNNNNNNd`          `dNNNNNNNNNNNNNNNNNNNNmy/`                 
                                 .+hmmNNNNNNNNNNNNNNNNd`          `dNNNNNNNNNNNNNNNNmmh+.                    
                                    ./sdmNNNNNNNNNNNNNd`          `dNNNNNNNNNNNNNmds/.                       
                                        ./ohmmNNNNNNNNd`          `dNNNNNNNNmmho/.                           
                                            `.:+sydmmNd`          `dNmmdys+:.`                               
                                                    `.-            -.`                                       

"""
if __name__ == '__main__':
    main()

Expert Answer

Explanation / Answer

lets understand line by line

1) global print

have a look at this example, i am writting :

my_var = 5

def fun1():

my_var=10

def fun2():

print(my_var)

fun1() #call fun1()

fun2() #call fun2()

If we run this , the output which is printed by fun2() will be 5.

This is because the value of my_var is 5 and fun2() doesnt have its own local my_var, so it takes up the global value.

So to use the global variable, everywhere we need to use the "global" keyword.

my_var = 5

def fun1():

global my_var=10

def fun2():

print(my_var)

fun1() #call fun1()

fun2() #call fun2()

Output:

10.

2) old_print = print

copying the value of global print variable into old_print variable which is not global.

3) def new_print(*args, **kwargs):
        pass

  *args and **kwargs allow us to pass a variable number of arguments to our neW_print over here.

*args is used to send a non-keyworded variable length argument list to the function.

**kwargs allows you to pass keyworded variable length of arguments to a function.

Here is an example to understand better:

Output:

Note: There is an order also, i.e we have three type and the order should be always like this only:

formal, variable lenght arguments, keyworded arguments

3) print = new_print

Whatever operation is performed inside the new_print() function, the value returned by the new_print() function is assigned to global print variable.

4) def patch():
        global print
        print = old_print

Here we have another function which declares the variable print globally, please note that even though it is declared inside the function body, it is not local because of global keyword , what it says is, we are referencing to the already declared global print variable.

5) def exit(code):

        patch()

    os._exit = exit

os._exit calls the function _exit() which does an immediate program termination.

Note the statement "can never return".

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote