-3

A number that equals to the sum of its own digits, where each digit raised to the power of number of digits. For example, 153 is an armstrong number because 1^3+3^3+5^3=153

at here user enters a number

    number=int(input("please enter a number: "))

Here in a while loop it puts the digits of the given number in numbers class

    numbers=[]
    while number>0:
        rem=number%10
        numbers.append(rem)
        number=int(number/10)

and then we want to put their qubes in qubes class

    qubes=[]
    for i in range(0,len(numbers)):
        c=(int(numbers[i]))**len(numbers)
        qubes.append(c)

and now we calculate the sum of the qubes class members

    result = sum(i for i in qubes)

I dont know why the if_code below doesnt work it just gives me false output I dont know why??

even when i enter 153 it prints false

    if result==number:
       print("true")
    else:print("false")

2 Answers2

2

To sum-up all suggestions from the comments:

Your main problem is:

  • When you create the numbers list you use number = int(number/10). This changes the number variable itself until it is equal to zero. This means that, as you experienced, result == number will always be False.

Some redundant parts of your code:

  1. See Splitting integer in Python? to get a list of a number's digits. Most commonly you can just do numbers = [int(i) for i in str(number)]. This will actually solve the problem above as you don't change number this way.

  2. The digits are already integers so no need for an int conversion. It is also more readable to use direct loop in Python rather than looping over indices:

    qubes = []
    for num in numbers:
        qubes.append(num**len(numbers))
    
  3. sum(i for i in qubes) is just an overly explicit way of saying sum(qubes).

  4. You are printing either "true" or "false" according to a boolean result. There is no need for a condition and you can simply print(result == number).


Putting together all the above, you can achieve this with a single line of code:

print(number == sum(int(digit)**len(str(number)) for digit in str(number)))
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
0

This is where you are wrong. you should iterate over the integer not the iterator given by range function

Use this

qubes=[]
for i in numbers:
  c=(int(i)**len(numbers))
  qubes.append(c)

instead of

qubes=[]
for i in range(0,len(numbers)):
  c=(int(numbers[i]))**len(numbers)
  qubes.append(c)

Also you are referencing the number to itself number=(int(number/10)) so your result value will be 153 but number value will be 0 at the end because you have reduced the value of number. So copy the value of number to another variable (num1 in below code).

Full code:

number=int(input("please enter a number: "))
num1 = number
numbers=[]
while number>0:
    rem=number%10
    numbers.append(rem)
    number=int(number/10)
    
qubes=[]
for i in numbers:
  c=(int(i)**len(numbers))
  qubes.append(c)
result = sum(i for i in qubes)
print(result == int(num1))
Prakash Dahal
  • 4,388
  • 2
  • 11
  • 25