Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

Ben Cope
1,269 PointsPrint triangular numbers up to 1000
Hi. For a task I have been set I have to use python to print all the triangular numbers up to 1,000. I have no idea how to do this, please help. Thanks
1 Answer

Vittorio Somaschini
33,371 PointsHello Ben,
this is actually quiet nice.
I presume you know what a triangular number is and how it is calculated!
Well, I did not know, so I had to look here: http://en.wikipedia.org/wiki/Triangular_number
These numbers look nice for real!
So, what I would do is creating a function with a limit (1000 in our case) and print all the numbers n = n * (n+1) / 2 using a for loop. (only where n < limit)
Does it make sense to you?
Vittorio
Ben Cope
1,269 PointsBen Cope
1,269 PointsHi Vittorio thanks for the help, although I am running into an error. Here is my code:
When I run it in the console, all the returns is an endless string of:
1.0 1.0 1.0 1.0
And it wont even print finish, please help
Vittorio Somaschini
33,371 PointsVittorio Somaschini
33,371 PointsHey!
I see there is a little bit of confusion in your code here; let's split the problem in 2 parts:
1 --> the mathematical aspects
the function for calculating the triangular number (wikipedia teaches us) is:
triangular_number = n * (n+1 ) / 2
So, make sure you correct that piece of code as your formula looks different to me.
2 ---> the code:
you have created an infinite loop here as the condition x < 1000 is ALWAYS TRUE. This is not what we want as this will crash our browser or software most likely. The result of your formula in always 1.0 and it gets printed an endless number of times!
I think you should use different variables for the triangular number and the condition check, before only using "x" complicates the whole thing and easily leads to mistakes.
PS: I have edited your code for readability, you may want to check out this page for properly posting code on the forum: https://teamtreehouse.com/forum/posting-code-to-the-forum
Ben Cope
1,269 PointsBen Cope
1,269 PointsThanks for all the help, I managed to complete the challenge with this:
Vittorio Somaschini
33,371 PointsVittorio Somaschini
33,371 PointsCool Ben!
It looks nice, if I may I suggest you to print number in int format, just print(int(x)) since they will all be integers they would look nicer. And also I have just read they are a sum of natural numbers (whole numbers), so they don't want decimals.
Good job!! Wonderful Numbers!
Vittorio