A DAY AT THE SUPERMARKET Lists + Functions 4/13


#1

I am really stuck on what to do next. here is my code.
def fizz_count(x):
count=0
for item in x:
if x==“fizz”:
count=count+1
return count
any tips would be greatly appreciated.


#2

Can you tell us what error messages are raised, and what it is you don’t understand?

Also, please furnish a link to the exercise. Thanks.


#3

ya sorry the error is fizz_count([‘fizz’, ‘buzz’]) returned None instead of the correct answer: 1


#4

Did you mean to write,

if item == 'fizz'

?


#5

i changed it to the item==‘fizz’ i get this error

fizz_count([‘fIzZ’, ‘fIzZ’, ‘fuzz’, ‘fizz’, 9, 1, ‘fizz’]) returned 1 instead of the correct answer: 2


#6

Show us your revised code, please.


#7

def fizz_count(x):
count=0
for item in x:
if item==‘fizz’:
count=count+1
return count
the error is fizz_count([‘fIzZ’, ‘fIzZ’, ‘fuzz’, ‘fizz’, 9, 1, ‘fizz’]) returned 1 instead of the correct answer: 2


#8

Is your return statement inside the loop?


#9

yes. would it have to do with the count equalling 0 and count+1?


#10

The count would return what had been set up to that point. Since it gets incremented by 1, then it will return that if the return statement is in the loop. The return should not be inside the loop, which needs to complete all iteration of the x object.


#11

i just rewrote it and heres the code
def fizz_count(x):
count=0
for item in x:
if item==‘fizz’:
count=count+1
return count
now the message im getting in the console is this
File “python”, line 7
SyntaxError: ‘return’ outside function


#12

That means you took away all the indentation. The return statement should have block indent (four spaces).


#13

oh sorry it doesn’t show the indentations when i copy paste. here i will copy paste with indents

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

it also has the same error of File “python”, line 7
SyntaxError: ‘return’ outside function


#14

The for loop is not inside the function. Add four spaces to the front of each of those lines. The return line leave alone. It is correct.


#15

heres the code now
def fizz_count(x):
count=0
for item in x:
if item==‘fizz’:
count=count+1
return count
i now got this message
File “python”, line 4
for item in x:
^
IndentationError: unexpected indent


#16

Have you read any of the articles in the new user section? Perhaps that would help with posting code samples and blockquotes in the forums. I’m not going to repeat it here, but dig around and you’ll find the FAQ’s on the subject.


#17

i got it. I just needed to move the for item in x back 2 spaces


#18

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.