In the code below identify the local and global variables Fo

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 ) )

Solution

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 ) )

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
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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site