Python global variable

前端 未结 6 1383
孤城傲影
孤城傲影 2020-12-16 14:49
def say_boo_twice():
  global boo
  boo = \'Boo!\'
  print boo, boo

boo = \'boo boo\'
say_boo_twice()

The output is

Boo! Bo

6条回答
  •  天涯浪人
    2020-12-16 15:37

    You are re-assigning boo after you declare it as global, so the value is the last one you assigned to it. If you removed line three, you would get the output you expect.

提交回复
热议问题