Home > Enterprise >  Counting the number of 6's and 8's in a range
Counting the number of 6's and 8's in a range

Time:11-12

I want to create a code to count the number of times 6 and 8 appears in a range, but I don't want to count any numbers that contain both 6 and 8. So far I have something like this:

def countnums(a) :
    count = 0 
    for x in range(a):
        if 6 and 8 in a:
            count -= 1
        elif 6 in a:
            count  = 1
        elif 8 in a:
            count  = 1
        return count

countnums(88)

However I'm not sure where I'm going wrong with this (sorry I'm quite new to Python). Any help will be deeply appreciated, as well as any referrals to tutorials!

CodePudding user response:

There are several issues:

  • Your requirement specifies 6 and 7 but your code looks at 6 and 8

  • You are using x to go through the range but all your conditions are looking at a

  • The in operator cannot find a digit in an integer so if 6 in a will not work. You would need to process that as strings (and use x): if '6' in str(x)

  • Combining conditions like 6 and 8 in a does not work the way you're expecting it to. You would have to break that down into two comparisons that you join with and: e.g. if '6' in str(x) and '8' in str(x)

  • Because you are using elif for the individual in checks after the combined one, you don't need to subtract 1 from the count (the count will not be affected by the other conditions).

  • You need to return the count only after to loop is finished (your indentation makes it end prematurely

Here's a revised version:

def countnums(a) :
    count = 0 
    for x in range(a):
        has6 = '6' in str(x)
        has8 = '8' in str(x)
        count  = has6 ^ has8  # Exclusive OR (True counts as 1, False as 0)
    return count

As a side note, you do realize that countnums(88) will be processing numbers from 0 to 87 right?

CodePudding user response:

There are multiple things that you got wrong here.

First, what you want to do is check, for each number x in between 0 and a if this number contains 6 or 8 as a digit. So, the number for which you should test in each loop cycle is x and not a. Also, the return statement shouldn't be inside the loop, or else your function returns only after one loop cycle. That gives us

def countnums(a) :
    count = 0 
    for x in range(a):
        if 6 and 8 in x:
            count -= 1
        elif 6 in x:
            count  = 1
        elif 8 in x:
            count  = 1
    return count

countnums(88)

Second, you used the conditional expression (6 in x). "in" is an operator than can take different types of operands. For example (a in [5,6,7]) returns true only if a is equal to an element of the list, and ("hello" in "hello, world") returns true only if the left operand is a substring of the right one. Now you used (8 in x) to express "the digit 8 appears in the decimal form of the number x". Unfortunately, the operator in is not defined for integer operands, so (8 in x) is incorrect. What you can do to correct that is to express this in the terms of a string, str(x) gives you the decimal string representation of x so you can do ("8" in str(x)). Which gives us :

def countnums(a) :
    count = 0 
    for x in range(a):
        if '6' and '8' in str(x):
            count -= 1
        elif '6' in str(x):
            count  = 1
        elif '8' in str(x):
            count  = 1
    return count

countnums(88)

Thirdly, you wrote ('6' and '8' in str(x)). Unfortunately, python understands that as ('6') and ('8' in str(x)), and knowing that '6' is evaluated as true (a nonempty string is always true), what python understands from your expression is just ('8' in str(x)) The correct way to express what you wanted to express is writing ('6' in str(x) and '8' in str(x)) Correcting that, we get :

def countnums(a) :
    count = 0 
    for x in range(a):
        if '6' in str(x) and '8' in str(x):
            count -= 1
        elif '6' in str(x):
            count  = 1
        elif '8' in str(x):
            count  = 1
    return count

countnums(88)

Lastly, when you use the elif statements, they are evaluated only if the previous statement is evaluated to be false. So in your case, for x being 268 for example, the first if statement is evaluated to be true, the count is decremented, but since we executed the first if block, the two latter won't even be evaluated, and so the count won't be incremented twice. To correct that, you must use three separate if statements:

def countnums(a) :
    count = 0 
    for x in range(a):
        if '6' in str(x) and '8' in str(x):
            count -= 1
        if '6' in str(x):
            count  = 1
        if '8' in str(x):
            count  = 1
    return count

countnums(88)

At this point the program works. But we can do one additional simplification : a boolean in python can be manipulated as a number. For example, False is equal to zero, True True is equal to 2, and True False is equal to one. So what we can do is :

def countnums(a) :
    count = 0 
    for x in range(a):
        if ('6' in x)   ('8' in x) == 1:
            count  = 1 
    return count

countnums(88)
  • Related