this code is for the ps3a "the first problem of problem set3: def match2(target,key): counter = 0 if find (target,key)==-1: return else: target = target[0:find(target,key)]+target[find(target,key)+len(key):len(target)+1] print target match2(target,key) counter=counter+1 print counter but i don't understand why count doesn't want to add up ?? help please (:
Every time your function is recursively called, counter is reset to 0 due to the line: counter = 0 There are a couple of ways to fix it: one, declare counter outside the function, and add each time. The second way is to declare match2 as match2(target, key, counter = 0) then before calling it recursively, just do counter+=1, and pass it as an argument as match2(target,key,counter)
thanks (: just before you answered me i figured it out (: but thanks anyway(:
sorry but the first way "declaring outside" didn't work it gives me "local variable not declared" ??
Use the global keyword, like count = 0 outside, then inside the function global counter.
thanks soooooo much!!
Join our real-time social learning platform and learn together with your friends!