Billy Thompson
Billy Thompson

Reputation: 459

Checking to see if a number ends in 5?

I'm trying to define a function that takes 2 parameters, adds them up, and if the sum of the two parameters ends in 5, it reports a 2. If it doesn't end in 5, it returns 8.

Any ideas?

I was thinking of doing an if statement, but I'm confused as to how I would check if a number ends in 5( or is 5).

Thanks for your help, trying to teach myself how to program is so difficult yet so rewarding :)

Upvotes: 2

Views: 22840

Answers (6)

Tadeck
Tadeck

Reputation: 137420

Solution

My answer assumes you are checking integers (which seems pretty reasonable judging from your question):

def sum_ends_with_5(a, b):
    """
    Checks if sum ends with "5" digit.
    """
    result = a + b
    return 2 if result % 10 == 5 else 8

or more flexible (with any number of arguments):

def sum_ends_with_5(*args):
    """
    Checks if sum ends with "5" digit.
    """
    result = sum(args)
    return 2 if result % 10 == 5 else 8

How it works (aka tests)

The function behaves like that:

>>> sum_ends_with_5(5)
2
>>> sum_ends_with_5(3)
8
>>> sum_ends_with_5(2, 8)
8
>>> sum_ends_with_5(7, 8)
2
>>> sum_ends_with_5(10, 20, 3, 2)
2

Shorter version

So, if you want to write it in shorter and more flexible way, you can do this:

def sum_ends_with_5(*args):
    return 2 if sum(args) % 10 == 5 else 8

Upvotes: 12

Levon
Levon

Reputation: 143102

One easy approach is to take the number and convert it to a string and check the last digit using indexing to see if it is 5:

E.g.,

n = 153
str(n)[-1] == '5':
False

and

n = 155
str(155)[-1] == '5'
True

So as part of an if-statement:

if str(n)[-1] == `5`: 
   print "number ends in 5" 
else: 
   print "number did not end in 5" 

If you just wanted to check for divisibility by 5 (which is different than ending with 5) you could use the mod operation.

But you also could mod by 10 and check for a remainder of 5 to determine if the number (int) ends with 5. My solution checks for the last digit of any number (including floats)

Upvotes: 2

paxdiablo
paxdiablo

Reputation: 882068

I like the solution from Tadeck best but there is another way, not as good in my opinion for this specific use case, but still may be useful if your return values ever need to follow more complex rules than is available from a simple modulo operation.

def xlatVal (*nums):
    #            0 1 2 3 4 5 6 7 8 9
    lookupTbl = [8,8,8,8,8,2,8,8,8,8]
    return lookupTbl[sum(nums) % 10]

While the values are still reduced to a range using modulo, this allows arbitrary translations across that range.

Upvotes: 1

cheeken
cheeken

Reputation: 34675

Numbers end in 5 if and only if they are are divisible by 5 but are not divisible by 10. You can easily check for these conditions with modulo arithmetic. More generally, you can check if a number ends with a digit by comparing the mod 10 value of that number to the digit.

num = 1234
isDivisibleByFive = num % 10 == 5

Upvotes: 4

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799110

Take the modulus by 10 and check if it's 5.

print num % 10 == 5

Upvotes: 5

Waleed Khan
Waleed Khan

Reputation: 11467

Convert it to a string and check the last character:

str(num)[-1] == "5"

Upvotes: 0

Related Questions