Syntax error 'return' External function in python

I am trying to count the word fizz using python. However, this gives me an error.

def fizz_count(x): count =0 for item in x : if item== "fizz": count=count+1 return count item= ["fizz","cat", "fizz", "Dog", "fizz"] example= fizz_count(item) print example 

i is checked indented, but it still doesn't work. Where am I doing wrong?

-2
source share
5 answers

Your indentation seems to be wrong, and you shouldn't have the first return count (why will you return count as soon as you define it?).

 def fizz_count(x): count = 0 for item in x: if item == "fizz": count += 1 # equivalent to count = count + 1 return count item = ["fizz", "cat", "fizz", "Dog", "fizz"] example = fizz_count(item) print example 
+1
source

Please try the following code: delete return count immediately after count = 0

There are also several retreat changes.

 def fizz_count(x): count = 0 for item in x: if item== "fizz": count=count+1 return count item = ["fizz","cat", "fizz", "Dog", "fizz"] example = fizz_count(item) print example 
+1
source

the problem is the identification in your return string

Try the following:

 def fizz_count(x): count =0 for item in x : if item == "fizz": count += 1 return count 
+1
source

You do not need the first return statement in the code. It works as follows, with fixed indentation and spacing:

 def fizz_count(x): count = 0 for item in x: if item == "fizz": count = count + 1 return count item= ["fizz","cat", "fizz", "Dog", "fizz"] example = fizz_count(item) print example 
+1
source

Well, I'm new to python world. What I found out is the return statement, there must be something like this.

Example one: -

 def split_train_test(data, test_ratio): shuffled_indices = np.random.permutation(len(data)) test_set_size = int(len(data) * test_ratio) test_indices = shuffled_indices[:test_set_size] train_indices = shuffled_indices[test_set_size:] return data.iloc[train_indices],data.iloc[test_indices] 

Example two: -

 def load_housing_data(housing_path=HOUSING_PATH): csv_path = os.path.join(housing_path, "housing.csv") return pd.read_csv(csv_path) 
0
source

All Articles