Hi I have coded the following for one of my python exercises in code academy.
def fizz_count(x):
count = 0
for item in x:
if item == fizz:
count = count + 1
return count
When I submit this code, I get the error : global name ‘fizz’ is not defined
But I tried adding the statement : global fizz inside the function. Still I get the same error. Please tell me where I am going wrong.
Your function shouldn’t be using a global variable, so the solution to the problem isn’t to create one.
If you read your code, you’re using fizz without having defined it - ask yourself where it should be coming from (and/or refer to the instructions)
Hi ionatan, ok I will not declare it as a global variable. so can you tell me how can I declare it ? Because even when I declare it outside the function like a = “fizz” or even inside the function, it throws the same error “global name ‘fizz’ is not defined”. I am guessing that I should declare the string ‘fizz’ as a variable outside the function ?
Python doesn’t know what your code is supposed to be. When it says that a variable doesn’t exist, consider whether it should exist, not where it should exist.
You’re using a variable by that name, you’re trying to read from a variable named fizz. If no such variable should exist, then you shouldn’t read from such a variable either. What were you meant to do? Compare to some text. That doesn’t require variables.