It runs expect when I add a print

l Hello, I am doing an exercise on Codecademy and I got the exercise right the goal was to count the length of a string without using len() but I decided to add a print at the end to know the result and it is saying that count is not defined and therefore it won’t work. I’m only getting this issue when trying to print(). This is my code. Thank you so much!

def get_length(string):

  count=0

  for index in string:

    count+=1

  return count

print(count)

count has a local scope, this variable only exists within the body of the function.

1 Like

thank you! does that mean i should have written count=0 outside of the function?

count should be inside the function. Otherwise the count sums multiple function calls.

you don’t even call the function?

ohh so i should have written print(get_length(“koala”))?

1 Like

I need 20 characters in my answer, so can’t just write yes. But the answer is: yes

but now it works as expected, right? so there is no real need to ask for confirmation

1 Like