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! While you're at it, check out some resources Treehouse students have shared here.

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

Python

Ben Cope
Ben Cope
1,269 Points

Print 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
Vittorio Somaschini
33,371 Points

Hello 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
Ben Cope
1,269 Points

Hi Vittorio thanks for the help, although I am running into an error. Here is my code:

x = 0
limit = 1000
while x<limit:
    x = ((x+1)/2) 
    print(x)
print("Finished")

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
Vittorio Somaschini
33,371 Points

Hey!

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
Ben Cope
1,269 Points

Thanks for all the help, I managed to complete the challenge with this:

# This will print triangular numbers up to 1,000!
n = 0
x = 1
while x<989:
    n = n + 1
    x = n * (n+1)/2
    print(x)
print("Finished")
Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Cool 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