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

In the code below, identify the local and global variables. For local variables,

ID: 3934467 • Letter: I

Question

In the code below, identify the local and global variables. For local variables, indicate the in which namespace that the variable has local scope. Write inline comments to indicate scope.

You are only required to add comments inline to indicate scope, no output or docstring is required.

def f( y ):

    print( 'f() local vars:', vars() )

  print()

    return x + y

def g( y ):

    global x

    x = 10

    print( 'g() local vars:', vars() )

    print()

    return x*y

def h( y ):

    x = 5

    print( 'h() local vars:', vars() )

    print()

    return y – 1

x = 5

res = f(2) + g(3) + h(4)

print( 'res = {0}'.format( res ) )

Explanation / Answer

def f( y ): //Here y has Local Scope

    print( 'f() local vars:', vars() )

  print()

    return x + y // x is global

def g( y ):

    global x

    x = 10 //x is global

    print( 'g() local vars:', vars() )

    print()

    return x*y

def h( y ):

    x = 5 //x is global

    print( 'h() local vars:', vars() )

    print()

    return y – 1 //y is global

x = 5

res = f(2) + g(3) + h(4)

print( 'res = {0}'.format( res ) )

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