4.Lists + Functions


#1



I have n o idea what's wrong.If i have a list shouldn't the exercise instructions say so?
Anyway this is the error :File "python", line 6
count = count += 1
^
SyntaxError: invalid syntax


# Write your function below!
def fizz_count(x):
    count = 0
    for x in fizz_count:
        if x == "fizz":
            count = count += 1
    return count


#2

You can use:

count = count + 1

or

count += 1

But not both together.


#3

Yes, you have both on the same line.


#4

Doent work.New error : Oops, try again. an exception was raised for fizz_count(['fizz', 'buzz']): 'function' object is not iterable


#6

Lol, I should've seen this line.


#7

I don't understand.Help


#8

for x in fizz_count:

lets see, fizz_count is the function, lets loop over the function? I would loop over the list: x (the parameter if the function):

for y in x:

#9

Correct but it doesn't print anything in the console.BTW in order to print something in the console i must add a list right?

# Write your function below!
def fizz_count(x):
    count = 0
    for item in x:
        if item == "fizz":
            count += 1
    return count
result = fizz_count(["fizz", 1, "fizz", 2, "fizz", 3, "fizz"])
print result

.Why does it work now(before and yesterday it didn't.And the day before yesterday)?
And how should i know that i had to add a list in order to print something?Or shouldn't i?Im' confused


#10

It does if you call print on the function call, or you store the result in a variable, and print the variable (which it seems you did)

In this case? yes.

You looped over your function, that doesn't work. So i guess that is why it didn't work earlier. Can't be sure

The code in your function is written to receive a list? Using a for .. in loop is used for lists. You can also write it different, so it would work for strings


#11

Yes but in this particular exercise i don't have to write a list right?If i want to only?


#12

No, you don't have to write the list. The exercise will test a list, but it is nice to see for yourself what you achieved :slight_smile:


#13

Aha ok.Thank you very much.I was stuck BTW with the same code for days.And today it worked.Thanks


#14

I am trying to add this code learned from this website to my python IDLE; however, for some reason it does not print. Can anyone help me out with this please! much appreciated!

for word in "Mahir Sultanov":
	if word == "Mahir":
		print("Mahir")

P.S I try all forms of printing

print (Mahir)
print Mahir
print("Mahir")


#15

Take a look at this code:

for word in "Mahir Sultanov":
   print(word)

its letter gets printed (the loop goes over each letter), a letter can never equal a whole name. If had a list:

for word in ["Mahir", "Sultanov"]:
	if word == "Mahir":
		print("Mahir")

it should work. So, looping over a list with for in will give you the items in the list, looping over a string with for in will give you each letter


#16

@orujgadimov A for/in loop doesn't work on a single string, you have to split it first.


#18

It did. Thank you, dantemp and stetim94, for your prompt response. Much APPRECIATED!!!!


#19

its wrong thats y its not work you print the result after the return fiction the programming goes through top to bottom when its come to fizz there is no list then it gave you an error bcs thers no list