quadratini7
quadratini7

Reputation: 17

Python Returns none instead of True or False

I'm making a function called isSiete() that will accept an integer from a txt file with 5000 random numbers.

Return True if the second column digit (The ‘tens’ column) of the number is a ‘7’ and False if it is not.

def isSiete(num):
    numString = str(num)
    numList = list(numString)
    numSum = 0
    for i in numList:
        if ('0' + i)[-2] == '7':
            return True
        else:
            return False

I expect the output to be True but I am getting False everytime. I've tried the following test numbers

isSiete(7777) isSiete(4774) isSiete(672)

Upvotes: 1

Views: 992

Answers (4)

Toothpick Anemone
Toothpick Anemone

Reputation: 4644

Your ('0' + i)[-2] is always equal to the character '0'

For example, suppose numList == ['A', 'P', 'P', 'L', 'E'] Suppose that i is an element of numList, such as 'P'

Then '0' + i == "0P"

[-2] gets the second-to-last character
"0P"[-2] == "0"

Note that it does not matter what P is. The second-to-last character of '0' + i is always '0'

('0' + i)[-2] == '7' will always return False


I encourage you to learn about the "modulus operator" (%)

x % 10 is the remainder of x divided by 10. For example, 74 % 10 == 4

In general x % y is the remainder of x when divided by y

To extract a specific digit out of a number, do the following:

def extract_digit(number, position):
    """
    position == 1 ......if you want the ones place
    position == 2 ......if you want the tens place
    position == 3 ......if you want the hundredths place
    position == 4 ......if you want the thousanths place
    and so on...
    """ 
    small_places = number % (10**position)
    digit = small_places //(10**(position - 1))
    return digit

For example, suppose you want the hundred's place of 123456789:

123456789 % 1000 == 789      
789 // 100 == 7

As a final result, we have:

def isSiete(num):
    return extract_digit(num, 2) == 7

Upvotes: 1

chepner
chepner

Reputation: 531165

Don't bother with strings at all; it's an order of magnitude faster to divide by 10 twice.

def isSiete(num):
    return num // 10 % 10 == 7

The arithmetic gets slower as the size of num increases, but this is still faster when num is a 17-digit number.

Upvotes: 2

ipeternella
ipeternella

Reputation: 93

I'm not sure if you asked for help to debug your code or if you wanted some help with a working solution.

In the case you wanted a solution: here's a working snippet which may help you achieve what you wanted.

def is_siete(num):
    """
    Asserts that the tens of a number is 7. Returns False if not.
    """
    num_as_str = str(num)

    try:

        second_num_as_str = num_as_str[-2]
        second_num_as_int = int(second_num_as_str)

        if second_num_as_int == 7:
            return True

        return False

    except IndexError:
        return False


if __name__ == "__main__":
    print(is_siete(7777))  # True
    print(is_siete(4774))  # True
    print(is_siete(672))  # True
    print(is_siete(17))  # False
    print(is_siete(7))  # False

Upvotes: 0

Omari Celestine
Omari Celestine

Reputation: 1435

You can simply use the converted string to check for your condition as a string in python works as an array of characters:

def isSiete(num):
    numString = str(num)
    tensPosition = len(numString) - 2

    if tensPosition >= 0 and numString[tensPosition] == '7':
        return True
    else:
        return False

Upvotes: 0

Related Questions