Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Armstrong in minimum lines
#1
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
Quote
#2
Please put your code in Python code tags and errors error tags. You can find help on that here.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Delete minimum occurence in a string RavCOder 10 312 Nov-12-2019, 01:08 PM
Last Post: RavCOder
  Minimum size Amniote 8 476 Jul-10-2019, 02:58 PM
Last Post: nilamo
  Product of maximum in first array and minimum in second Thethispointer 9 1,191 Jan-19-2018, 07:38 PM
Last Post: Thethispointer

Forum Jump:


Users browsing this thread: 1 Guest(s)