[SOLVED] Function is not returning any values

Issue

I am coding to find whether or not a number is a disarium number or not. A disarium number is a number in which it’s digits can be raised to the power of their respective positions and added to give the number.

Example1: 75 is not a disarium number, because 7^1 + 5^2 = 32 and not 75

Example2: 135 is a disarium number, since: 1^1 + 3^2 + 5^3 = 1 + 9 + 125 = 135

In my code, even though 75 is not a disarium number, nothing is returned. It’s like my function isn’t even going through my if and else statements

def is_disarium(number):
    number = str(number)
    selection_array = [int(num) for num in number]
    disarium_check = 0

    for i in range(len(selection_array)):
        disarium_check += int(selection_array[i])**(i+1)

    if disarium_check == number:
        return True
    else:
        return False

is_disarium(75)

My code (image)

Solution

This code is currently returning False for every number because in the if statement you are comparing an integer with a string. And you can use the print command to find the return value.
Use this modified code to check whether it is working as expected.

def is_disarium(number):
    number = str(number)
    selection_array = [int(num) for num in number]
    disarium_check = 0

    for i in range(len(selection_array)):
        disarium_check += int(selection_array[i])**(i+1)

    if disarium_check == int(number):
        return True
    else:
        return False

output = is_disarium(135)
print(output)

Answered By – Nuvindu Nirmana

Answer Checked By – Timothy Miller (BugsFixing Admin)

Leave a Reply

Your email address will not be published.