Python for loop error

前端 未结 3 1193
盖世英雄少女心
盖世英雄少女心 2021-01-28 17:38

I\'m working on a Python exercise at Codecademy and got stuck on what looks like a simple problem:

Write a function fizz_count() that loops t

3条回答
  •  攒了一身酷
    2021-01-28 18:11

    If x is a sequence of elements, when you do

    for i in x:
    

    you are looping through the elements of x, not through indexes.

    So when you do

    x[i]
    

    you are doing

    x[element]
    

    which makes no sense.

    What can you do?

    You can compare the element with 'fizz':

    for element in x:
        if element == 'fizz':
            # ...
    

提交回复
热议问题