Reputation: 2897
I am having a hard time to fix my problem. I made this:
def thereyougo():
"""
The function thereyougo() should return a list with all the numbers from
1200 upto and including 2399 that are divisible by 12.
"""
i = range(1200, 2400)
z = []
for k in i:
if k % 12 == 0:
z.append(k)
print z
thereyougo()
The problem is, that I get a whole bunch of lists, but I want just one.
For example, this is the output I get:
[1200]
[1200, 1212]
[1200, 1212, 1224]
[1200, 1212, 1224, 1236]
While I want [1200, 1212, 1224... ]
What am I doing wrong?
Upvotes: 0
Views: 24
Reputation: 20434
Just un-indent
the print
:
for k in i:
if k % 12 == 0:
z.append(k)
print z
Note that you could do this more efficiently by using the step
param of the range()
generator. This would allow you to get every 12th
number which would, by definition, be every multiple
.
Using the above, you could reduce your function down to:
def thereyougo():
print list(range(1200, 2400, 12))
which, when called, would give:
[1200, 1212, 1224, 1236, 1248, 1260, 1272, 1284, 1296, 1308, 1320, 1332, 1344, 1356, 1368, 1380, 1392, 1404, 1416, 1428, 1440, 1452, 1464, 1476, 1488, 1500, 1512, 1524, 1536, 1548, 1560, 1572, 1584, 1596, 1608, 1620, 1632, 1644, 1656, 1668, 1680, 1692, 1704, 1716, 1728, 1740, 1752, 1764, 1776, 1788, 1800, 1812, 1824, 1836, 1848, 1860, 1872, 1884, 1896, 1908, 1920, 1932, 1944, 1956, 1968, 1980, 1992, 2004, 2016, 2028, 2040, 2052, 2064, 2076, 2088, 2100, 2112, 2124, 2136, 2148, 2160, 2172, 2184, 2196, 2208, 2220, 2232, 2244, 2256, 2268, 2280, 2292, 2304, 2316, 2328, 2340, 2352, 2364, 2376, 2388]
Upvotes: 2