+ 1

Recently I built a program but it has a little problem, PYTHON

https://code.sololearn.com/cY0Q87P2mKb9/?ref=app The program works completely fine but when it come to numbers which have a zero(0) in them it prints the number twice. I don't know why. For example: 370 is an Armstrong number but the program prints this number twice. Any suggestions on how to fix it would be greatly appreciated! I've already googled through this program but they use another logic. That logic is good but do require some time to learn about while mine program for same is simple and easy to understand. Please help to fix this little error.

31st Jul 2019, 3:52 PM
Rohit Panchal
Rohit Panchal - avatar
10 Answers
+ 4
👆 this is what I meant Instead of checking throughout the loop check after the end of inner loop Intermediate steps won't matter so checking after every iteration is useless.
1st Aug 2019, 2:28 AM
‎ ‏‏‎Anonymous Guy
+ 2
‎ ‏‏‎Sreejith  wow thanks.. really appreciated the way expressed the solution. Now I am really wondering what a fool I was to not think about it.. thanks a lot.
31st Jul 2019, 5:01 PM
Rohit Panchal
Rohit Panchal - avatar
1st Aug 2019, 5:31 AM
Rohit Panchal
Rohit Panchal - avatar
+ 1
So what happens in your inner for loop is: 1. Selects number from main loop (i) and loops according to the no. of digits present in (i) 2. The the loop multiplies individual digits to itself n no. of times (where n = no. of digits in i). 3. If the answer of above calculation is equal to i then it's displayed Now in case of 370 (or any Armstrong no. with zero at end) inner loop iterates 3 time Loop 1 3 ^ 3 = 27 27 not equal to i so not displayed Loop 2 7 ^ 3 = 343 27 + 343 = 370 It is equal to i so displayed You want your program to end here Loop 3 0^3 = 0 370 + 0 = 370 So displayed To avoid this you could add break in if block to make sure the inner loop terminate when the answer is printed
31st Jul 2019, 4:48 PM
‎ ‏‏‎Anonymous Guy
+ 1
Well break statement do fix the problem but in this specific case only.. suppose a 3 digit no.(xyz) In which the initial digits only do satisfy the condition like: x^3 + y^3 it will print the output but suppose z in not 0. So it will return it, even though it is not a Armstrong number... Any way to fix this? ‎ ‏‏‎Sreejith 
31st Jul 2019, 5:07 PM
Rohit Panchal
Rohit Panchal - avatar
31st Jul 2019, 5:08 PM
Rohit Panchal
Rohit Panchal - avatar
+ 1
~ swim ~ need your help once again bro.
31st Jul 2019, 5:08 PM
Rohit Panchal
Rohit Panchal - avatar
+ 1
Rohit Put the if block outside inner for loop line 7-9
31st Jul 2019, 5:13 PM
‎ ‏‏‎Anonymous Guy
+ 1
‎ ‏‏‎Sreejith  well it won't give any output
31st Jul 2019, 5:36 PM
Rohit Panchal
Rohit Panchal - avatar
+ 1
If it is outside the inner loop
31st Jul 2019, 5:36 PM
Rohit Panchal
Rohit Panchal - avatar