Quiz Catalog

Catalog of quizzes

def foo():
  x = 1
  return x

foo.x = 4

print(foo(), foo.x)
1 4
  • scope
  • object/variable
  • function/variable
  • function/object

def foo():
  x = 300
  def foo2():
  print(x)
  foo2()

foo() 
300
  • scope

x = 300

def foo():
  return x

print(foo() == x)
True
  • scope
  • boolean/operation/compare

x = 300

def foo():
  x = 200
  return x

print(foo(), x)
200 300
  • scope

def foo():
  global x
  x = 300

foo()

print(x)
300
  • scope
  • statement/global

x = 300

def foo():
  global x
  x = 200

foo()

print(x) 
200
  • scope
  • statement/global

def foo():
  x = 12
  def foo2(a):
  x += a
  return x
  return foo2

fun = foo()

print(fun(2))
UnboundLocalError: local variable 'x' referenced before assignment
  • scope

def func(): 
  print(name, end = ' ')
  name = "Anton"
  print(name)

name = "Petr"
func()
UnboundLocalError: local variable 'name' referenced before assignment
  • scope

name = 'Anton'

def greeting():
  name = 'Rob'
  print(f'Hello {name}', end = ' ')

def farewell():
  print(f'Bye {name}')

greeting()
farewell()
Hello Rob Bye Anton
  • scope

num = 2

def increment():
  num = num + 1
  print(num)

increment()
UnboundLocalError: local variable 'num' referenced before assignment
  • scope

def outer():
  num = 5

  def inner():
    num = 25
    print(num, end = ' ')

  inner()
  print(num)

outer()
25 5
  • scope
  • function/inner