0

I already declare b = 6 globally. But why this code shows an error instead of printing b = 6.But when I delete b = 33 inside that is inside the function the code works well & print b = 6. But why?

b = 6
def f(a):
      print(a)
      print(b)
      b = 33

f(20)
nobin50
  • 9
  • 3
  • Use `nonlocal b`... – Willem Van Onsem Jul 08 '18 at 09:36
  • Hi @John Zwinck, I searched for it but can not find. Can you help me with the link? Because I am totally new here. – nobin50 Jul 08 '18 at 09:36
  • 1
    @John Zwinck I don't think that this is a duplicate of https://stackoverflow.com/questions/423379/using-global-variables-in-a-function. This asks about a specific behaviour, not about the use of globals in general. – Insomniac Jul 08 '18 at 09:39
  • 2
    @Insomniac: it is, and of https://stackoverflow.com/questions/41369408/how-to-change-a-variable-after-it-is-already-defined-in-python. The OP really should have *at least* included the error message in their question. – Martijn Pieters Jul 08 '18 at 10:23

0 Answers0