Python Forum
Armstrong in minimum lines - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Armstrong in minimum lines (/thread-12618.html)



Armstrong in minimum lines - Gaurav - Sep-03-2018

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/Armstrong.py", line 2, in <module>
list = [int(i**3) for i in str(num)]
File "C:/Users/Gaurav/PycharmProjects/Armstrong/Armstrong.py", 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


RE: Armstrong in minimum lines - j.crater - Sep-03-2018

Please put your code in Python code tags and errors error tags. You can find help on that here.