Python Forum

Full Version: Printing out a triangle using nested for loops
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
So I encountered this question : I have to print out a diamond shaped diagram(shown below). How and where do I start ? Any hints ? I am lost completely, don't even know where to start ...


*
***
*****
***
*
You are going to need a loop. Either a for loop or a while loop would do. You'll need a condtional (if/else) to decide when to start subtracting from the loop index (for loop) or decrementing instead of incrementing the loop index you are keeping track of your self (while) loop. Note that '*' * 3 = '***'. Once you have the right index, you'll need that to display the diamond.
Normally don't like to point to other source however this is well done:
In addition to Ichabod's answer, see: https://stackoverflow.com/a/32613884
Use the two equations show to determine number of '*' vs number of spaces for each line
(Jan-15-2019, 05:30 PM)Larz60+ Wrote: [ -> ]Normally don't like to point to other source however this is well done:

I don't know if I would call that well done. The formulas are useful, but the single letter variable names and the odd use of string formatting syntax make the code rather confusing.
That's why I included:
Quote:Use the two equations show to ...
I should have been more specific.
Thanks alot, I managed to find the solution. Here it is :
i = 0

while i <= 4:
    if i <= 2:
        print (("*" * i * 2) + "*")
    else:
        print ((9 - 2*i) * "*")

    i = i + 1
Result :
Output:
* *** ***** *** *
Please use python and output tags when posting code and results. I put them in for you this time. Here are instructions for doing it yourself next time.

I think you need to print spaces before the lines, so each line is centered, like this:

Output:
* *** ***** *** *
And I expect your professor will want it to work for any size specified. Right now it only works for a size of 3. I would go back and check the original question to make sure you are fully answering it.
So, I am supposed to print the below diagram out using nested for loops :

*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*

I managed to write a piece of code that could print it out , but not sure if it constitutes using nested for loops. I put 2 for loops inside a while loop, does that count ? Here's the code below :

i = 1
while i <= 9:
for i in range(1, 6):
print ("*" * i)
for i in range(6, 10):
print ((i - 2 * (i -5)) * "*")
i = i + 1
I got this warning from the admins : User has been warned for this post. Reason: No BBcode, Creating new threads unnecessarily

What does it mean ?
(Jan-16-2019, 08:28 AM)MrGoat Wrote: [ -> ]I got this warning from the admins : User has been warned for this post. Reason: No BBcode, Creating new threads unnecessarily

What does it mean ?

Exactly what it means - you post code without using proper BBcode tags even after you have been advised to use them. And also you post new thread unnecessarily. You should keep the discussion in the original thread. The warning will expire in 1 week, but if you get another warnings for breaking the forum rules the warnings may accumulate.
Pages: 1 2