Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Armstrong in minimum lines
Hey guys. I've just started learning python. Made a simple armstrong program and need help on it.
Basically I wanna know how to nest for loops.
Here goes my program

num = 371 #int(input("Enter a number:"))
list = [int(i) for i in str(num)]
pow = [int(d**3) for d in list]
print("YES" if sum(pow) == num else "NO")

My problem is :
1. I have to create list of the num variable to split the numbers
2. Then I have to find power of 3 of each num in the list

I want to do both the things in one line. Maybe somewhat like

list = [int(i**3) for i in str(num)]

But this gives me an error that says :

Traceback (most recent call last):
File "C:/Users/Gaurav/PycharmProjects/Armstrong/", line 2, in <module>
list = [int(i**3) for i in str(num)]
File "C:/Users/Gaurav/PycharmProjects/Armstrong/", line 2, in <listcomp>
list = [int(i**3) for i in str(num)]
TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'

Any help is appreciated.
#Sorry for my bad english though LOL
Please put your code in Python code tags and errors error tags. You can find help on that here.

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Iterate 2 large text files across lines and replace lines in second file medatib531 13 392 Aug-10-2020, 11:01 PM
Last Post: medatib531
  Finding MINIMUM number in a random list is not working Mona 5 494 Nov-18-2019, 07:27 PM
Last Post: ThomasL
  Delete minimum occurence in a string RavCOder 10 696 Nov-12-2019, 01:08 PM
Last Post: RavCOder
  Minimum size Amniote 8 725 Jul-10-2019, 02:58 PM
Last Post: nilamo
  Watson Personality Insight: minimum number of words kiton 0 1,045 Apr-28-2018, 03:28 PM
Last Post: kiton
  Product of maximum in first array and minimum in second Thethispointer 9 1,591 Jan-19-2018, 07:38 PM
Last Post: Thethispointer
  how to determine the minimum system requirment for the game i am creating? hsunteik 1 1,484 Mar-25-2017, 12:36 PM
Last Post: metulburr

Forum Jump:

Users browsing this thread: 1 Guest(s)