Skip to content
CodeKruPro

Changing a global counter inside a function

QuizMediumPython onlyAsked in: TCS

The variable total is global: it is written at the top of the file, outside any function. What happens when bump() is called?

total = 0

def bump():
    total += 1

bump()
  • UnboundLocalError: cannot access local variable 'total'
  • total becomes 1
  • NameError: name 'total' is not defined
  • A new local total is created with the value 1

This question is part of Pro+

Pro+ opens every question in Interview Prep and the mock interview. Every article and the first 2 questions of each topic stay free. Hands-on Selenium and API tasks follow their course lesson's own lock.

See Pro+

Already have Pro+? Sign in