Computer Science

Consider the code given below:

b = 100
def test(a):
............... #missing statement
    b = b + a
    print(a, b)
test(10)
print(b)

Which of the following statements should be given in the blank for #Missing Statement, if the output produced is 110?

  1. global a
  2. global b = 100
  3. global b
  4. global a = 100

Python Functions

3 Likes

Answer

global b

Reason — When we want to modify a global variable within a function in Python, we need to use the global keyword followed by the name of the global variable. In this case, since the variable b is declared as a global variable outside the function, we need to declare it as a global variable again inside the function using global b before attempting to modify its value.

Answered By

1 Like


Related Questions