Sep-03-2018, 03:38 PM
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
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
