Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 19 Online
OpenStudy (anonymous):

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 (:

OpenStudy (anonymous):

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)

OpenStudy (anonymous):

thanks (: just before you answered me i figured it out (: but thanks anyway(:

OpenStudy (anonymous):

sorry but the first way "declaring outside" didn't work it gives me "local variable not declared" ??

OpenStudy (anonymous):

Use the global keyword, like count = 0 outside, then inside the function global counter.

OpenStudy (anonymous):

thanks soooooo much!!

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!