Reputation: 59
This is my code, the program is doing it's job but I don't understand why I don't get one unique list when I print. Why do I get each number as a separate list?
The code is easy to understand.
I have no clue how I could fit all my values in one list cz I actually don't even understand why I'm getting them in separate lists, even when I append them as integers and not as strings. Anyone could enlighten me a bit?
Thank you for your help,
Rouba
#################################
# Ex1: Nombres de Amstrong
# 20205793
# Créé le 14/03/2021
################################
for a in range(100):
for b in range(100):
for c in range(100):
#takes all the numbers from 0 to 99
nb = int(str(a)+str(b)+str(c))
#sums up the numbers as characters
sommeCubes=a**3+b**3+c**3
#sums up the numbers to the power 3 for each
liste=[]
#creation of an empty list
if nb==sommeCubes:
I tried liste.append(nb)
liste.append(str(nb))
print(liste)
#add to the list the numbers of which the sum of its numbers to the power 3 is equivalent to the number as a string
Output:
['0']
['1']
['153']
['2213']
['370']
['371']
['407']
['4160']
['4161']
['41833']
['1000']
['1001']
['165033']
['221859']
['341067']
['444664']
['487215']
['982827']
['983221']
[Finished in 1.8s]
Upvotes: 0
Views: 336
Reputation: 138
liste=[]
for a in range(100):
for b in range(100):
for c in range(100):
nb = int(str(a)+str(b)+str(c))
sommeCubes=a**3+b**3+c**3
if nb==sommeCubes: liste.append(str(nb))
print(liste)
Output :
['0', '1', '153', '2213', '370', '371', '407', '4160', '4161', '41833', '1000', '1001', '165033', '221859', '341067', '444664', '487215', '982827', '9
83221']
You need to initialise your list outside the loop.
Upvotes: 1