Python Forum
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
Reply
#2
Please put your code in Python code tags and errors error tags. You can find help on that here.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to get indices of minimum time difference Mekala 1 2,150 Nov-10-2020, 11:09 PM
Last Post: deanhystad
  How to get index of minimum element between 3 & 8 in list Mekala 2 2,519 Nov-10-2020, 12:56 PM
Last Post: DeaD_EyE
  Iterate 2 large text files across lines and replace lines in second file medatib531 13 5,825 Aug-10-2020, 11:01 PM
Last Post: medatib531
  Finding MINIMUM number in a random list is not working Mona 5 3,044 Nov-18-2019, 07:27 PM
Last Post: ThomasL
  Delete minimum occurence in a string RavCOder 10 3,927 Nov-12-2019, 01:08 PM
Last Post: RavCOder
  Minimum size Amniote 8 3,827 Jul-10-2019, 02:58 PM
Last Post: nilamo
  Watson Personality Insight: minimum number of words kiton 0 2,732 Apr-28-2018, 03:28 PM
Last Post: kiton
  Python find the minimum length of string to differentiate dictionary items zydjohn 3 3,615 Mar-03-2018, 05:23 PM
Last Post: Gribouillis
  Product of maximum in first array and minimum in second Thethispointer 9 5,276 Jan-19-2018, 07:38 PM
Last Post: Thethispointer
  how to determine the minimum system requirment for the game i am creating? hsunteik 1 3,063 Mar-25-2017, 12:36 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020