0

I am trying to complete this attempt to count how many times "Z" eats "E" in the string. In other words I need to count how many times "ZE" is in the given string:

letters = "GOLDZEZEDUZZEOZBIX"

Why is this code only returning 1?

def is_eaten(data):
    count = 0
    if "Z" and "E" in data:
        count += 1
        return count
tshepang
  • 12,111
  • 21
  • 91
  • 136
AAA
  • 39
  • 2
  • 6
  • 1
    This is a duplicate of http://stackoverflow.com/questions/1155617/count-occurrence-of-a-character-in-a-python-string – nietonfir Apr 29 '13 at 21:56
  • 1
    That question is counting single characters, not substrings. The chosen answer makes it pretty clear that it works just as well with substrings, which means it's exactly what the OP needs, so I voted to close. But I wanted to point that out in case others disagree. – abarnert Apr 29 '13 at 21:58
  • 1
    Actually, the OP isn't actually asking "how do I do this", but "why is this code give only returning 1", so… it actually _isn't_ a dup. – abarnert Apr 29 '13 at 22:06

4 Answers4

3

You can simply use the count method:

letters.count('ZE') # returns 3
aldeb
  • 6,588
  • 5
  • 25
  • 48
  • 1
    Short, simple, and Pythonic! – nemec Apr 29 '13 at 21:58
  • and almost certainly prohibited by the constraints of his homework :P – Joran Beasley Apr 29 '13 at 22:04
  • 2
    @JoranBeasley: Yep, but the OP didn't ask us for the answer to his homework, or describe the constraints. I tried to guess at them, but I think this answer is better (at least for the second half of the question), because it doesn't try to guess, it just answers what the OP asked. If he doesn't like this answer, he should fix his question. – abarnert Apr 29 '13 at 22:05
2

Because you set count to one if Z and E is in data. Not once for every ZE, but if.

Read up on for loops.

Lennart Regebro
  • 167,292
  • 41
  • 224
  • 251
2

Why is this code only returning 1?

Multiple reasons.

First, there's no loop in the code, so there's no way it could ever return more than 1.

Second, if "Z" and "E" in data doesn't mean what you think. It's true if "Z" is true, and also "E" in data is true. In other words, it's equivalent to if ("Z") and ("E" in data). But, even if the parentheses went the other way, and it were if ("Z" and "E") in data) it wouldn't work. Since "Z" and "E" is just "E" (try it and see), that would just be checking whether "E" appears in data.

You need if "Z" in data and "E" in data to do what you're trying to do.

Third, even if you fix that, the logic doesn't make any sense. This is true if there's any "Z" anywhere, and any "E" anywhere. So, it would be true for "EZ", which is obviously wrong. You only want it to be true if the substring "ZE" appears in the data, right? You can use if "ZE" in data to mean exactly that. Or, if you're not allowed to do that for some reason, first find a "Z" and check whether the next character is "E".


Here's a way you could do this (not a very good way, but the closest I could come up with to what you tried):

def count_eaten(data):
    count = 0
    while True:
        index = data.find('Z')
        if index == -1:
            return count
        data = data[index+1:]
        if data[0] == 'E':
            count += 1

Or, more simply:

def count_eaten(data):
    count = 0
    while True:
        index = data.find('ZE')
        if index == -1:
            return count
        data = data[index+1:]
        count += 1

Or, even more simply:

def count_eaten(data):
    return data.count('ZE')

I'm guessing your professor doesn't want this last one, and probably doesn't want the one before it either… but that's really just a guess (as is the fact that this is homework in the first place).

Kate Gregory
  • 18,808
  • 8
  • 56
  • 85
abarnert
  • 354,177
  • 51
  • 601
  • 671
  • 1
    Note that you can do a substring search using the `if .. in ..` syntax: `if "ZE" in data:`. – nemec Apr 29 '13 at 22:00
  • 1
    @nemec: I suspect this is a homework assignment where they've been given the usual stupid artificial constraints; otherwise, just use `count` in the first place. But yeah, I should mention that. – abarnert Apr 29 '13 at 22:01
0

for even another solution try this:

def keyCount(dataset, key):
    return dataset.count(key)

correct usage of this method would then look like:

>>> letters = "GOLDZEZEDUZZEOZBIX"
>>> key = "ZE"
>>> keyCount(letters, key)
3

or

>>> keyCount(letters, "ZE")
3

or

>>> keyCount("GOLDZEZEDUZZEOZBIX", "ZE")
3

etc..

Yup.
  • 1,883
  • 21
  • 18