0
def find_index(string):
'''(str) -> int
Return the index of the AND or OR symbol which is most outside
of the input string.
REQ: the first and last elements of the string must be "(" and
")" respectively.
>>> find_index("((-x+y)*-(-y+x))")
7
>>> find_index("(-x+y)")
3
'''
s = string[1:-1]
for i in range(len(s)):
left = s.count(L_BRACKET, 0, i)
right = s.count(R_BRACKET, 0, i)
if left - right == 0 and s[i] in AND + OR:
index = i + 1
return index
前に参照のローカル変数「指数は、」私は入力文字列、 の最も外側のシンボルを見つけるために書いた関数であるが、私は find_index(のようにそれをテストするとき「(X)」 )、それはNoneを返す必要がありますが、Pythonは builtins.UnboundLocalError私にエラーを与える:ローカル変数「インデックス」私は私のコードをimprveべきか、私は、Python 3builtins.UnboundLocalError:割り当てここ
:
はこれを試してみてください。デフォルト値、恐らく 'index = None'を先頭に使ってみてください。 –