function - Local variable referenced before assignment in Python -
truel="" count = 0 finle_touch=false #true after find first 3 upperletter # check if there 1 lower letter after 3 upper letter def one_lower(i): count=0 if == i.lower: finle_touch=true truel=i # check 3 upper letter def three_upper(s): in s: if count == 3: if finle_touch==true: break else: one_lower(i) elif == i.upper: count +=1 print(count) #for debug else: count ==0 finle_touch=false stuff="dsfsffsfsssfsfsffssfsssssssss......." three_upper(stuff) print(truel)
so got alot of string on 'stuff' , find 1 lowercase letter sorrund 3 uppercase letter.
but when run code get:
traceback (most recent call last): file "c:\python33\mypy\code.py", line 1294, in <module> three_upper(stuff) file "c:\python33\mypy\code.py", line 1280, in three_upper if count == 3: unboundlocalerror: local variable 'count' referenced before assignment
i don't understand why. in advance
due line count +=1
python thinks count
local variable , not search global scope when used if count == 3:
. that's why got error.
use global
statement handle that:
def three_upper(s): #check 3 upper letter global count in s:
from docs:
all variable assignments in function store value in local symbol table; whereas variable references first in local symbol table, in global symbol table, , in table of built-in names. thus, global variables cannot directly assigned value within function (unless named in global statement), although may referenced.
Comments
Post a Comment