0

I'm trying to add 1 to the count each time there's a new letter and keep track of the doubled letters, but python isn't going into the if statement, despite line 6 showing the conditions for the if statement are met. What am I missing?

def duplicate_count(text):
    count = 0
    doubled_letters = []
    for i in text:
        print (i)
        print(i in doubled_letters)
        if i in doubled_letters == False:
            count += 1
            doubled_letters.append(i)
    print(count)
    print(doubled_letters)
    return count

duplicate_count("abbcdea")

this returns:

a
False
b
False
b
False
c
False
d
False
e
False
a
False
0
[]
FlyingTeller
  • 17,638
  • 3
  • 38
  • 53
aj1000
  • 23
  • 3

2 Answers2

7

in is a comparison operator, so it gets chained with ==. Your expression is equivalent to

(i in doubled_letters) and (double_letters == False)

You could "break the chain", so to speak, using explicit parentheses:

if (i in double_letters) == False:

but direct comparisons to Boolean literals are rarely, if ever, necessary. Just write

if i not in double_letters:

(Note that not in is a single, two-word operator, not an application of not to in. The equivalent with in would be not (i in double_letters).)

chepner
  • 497,756
  • 71
  • 530
  • 681
  • even if you need to use literals, `is True` is preferred over `== True` – Ankur S Jul 16 '18 at 13:39
  • 1
    Both are pretty terrible; I wouldn't bother trying to choose a preference. – chepner Jul 16 '18 at 13:40
  • 1
    @AnkurS `is True` tests for identity, so it may not yield the same results as ` == True`. I don't know where you've seen that it would be "preferred", as far as I'm concerned such a test would not pass a code review. – bruno desthuilliers Jul 16 '18 at 14:00
  • @brunodesthuilliers See https://stackoverflow.com/a/24846681/1126841; `True` and `False` are guaranteed unique, like `None`, so it would at least be safe, if still unnecessary, to use `is`. – chepner Jul 16 '18 at 16:04
0

I think your logic is also flawed in addition to the incorrect usage of the operator. Looks like you are trying to get a count of duplicated letters, but I don't think your program will be able to do it.

I guess you are expecting something like this:

def duplicate_count(text):
    count = 0
    letters = []
    for i in text:
        if i not in doubled_letters:
            letters.append(i)
        else:
            count += 1

    print(count)
    return count

duplicate_count("abbcdea")

In the above code, I am trying to add every letter to a list and if they are duplicated, I increase the count. Based on this, you should be getting 2 as a and b are duplicated in the input.

Jay
  • 24,173
  • 25
  • 93
  • 141
  • thanks, that's what my program is meant to do, but when I kept getting this error I took out all of the stuff that wasn't causing a problem so I could focus in on the important part to post here. The code above is just the problematic fragment. – aj1000 Jul 17 '18 at 07:30