0

I'm trying to create a program in a queue class here is my code:

        def count(self): #im having a problem with this def count
            vowels=0
            for i in self:
              if(i=='a' or i=='e' or i=='i' or i=='o' or i=='u' or i=='A' or i=='E' or i=='I' or i=='O' or i=='U'):
                vowels=vowels+1
            print(vowels)

       

I'm having a problem with counting the vowels the user entered. It says "line 44, in q.count()" and "line 16, in count for i in self: TypeError: 'queue' object is not iterable"

jbi2eb9ugb
  • 17
  • 1
  • 4

3 Answers3

0

Check how to create iterator : https://wiki.python.org/moin/Iterator

You have to implement __iter__() and __next__() which your class does not. That's why compiler tells you that 'queue' object is not iterable.

This answer show you how to implement an iterator properly in Python.

Maxouille
  • 2,729
  • 2
  • 19
  • 42
0

"Iterator in Python is simply an object that can be iterated upon. An object which will return data, one element at a time.

Technically speaking, a Python iterator object must implement two special methods, iter() and next(), collectively called the iterator protocol."

According to the doc above, your queue object is not iterable, for it to be, you should implement the methods mentioned above.

Hook
  • 355
  • 1
  • 7
-1

If object of your class is supposed to be iterable implement what Maxouille's answer say, if this is not requirement you might elect to just replace

for i in self:

using

for i in self.items:

in method count

Daweo
  • 31,313
  • 3
  • 12
  • 25