Python Forum
Printing out a triangle using nested for loops
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Printing out a triangle using nested for loops
#1
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 ...


*
***
*****
***
*
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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
Reply
#4
(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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
That's why I included:
Quote:Use the two equations show to ...
I should have been more specific.
Reply
#6
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:
* *** ***** *** *
Reply
#7
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#8
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

User has been warned for this post. Reason: No BBcode, Creating new threads unnecessarily
Reply
#9
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 ?
Reply
#10
(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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  reduce nested for-loops Phaze90 11 1,860 Mar-16-2023, 06:28 PM
Last Post: ndc85430
Sad triangle numbers Woody_MC_2022 5 1,188 Sep-24-2022, 08:14 PM
Last Post: deanhystad
  Nested for loops: Iterating over columns of a DataFrame to plot on subplots dm222 0 1,692 Aug-19-2022, 11:07 AM
Last Post: dm222
  Nested for loops - help with iterating a variable outside of the main loop dm222 4 1,565 Aug-17-2022, 10:17 PM
Last Post: deanhystad
  breaking out of nested loops Skaperen 3 1,207 Jul-18-2022, 12:59 AM
Last Post: Skaperen
  Break out of nested loops muzikman 11 3,316 Sep-18-2021, 12:59 PM
Last Post: muzikman
  How to break out of nested loops pace 11 5,309 Mar-03-2021, 06:25 PM
Last Post: pace
  Nested for Loops sammay 1 8,258 Jan-09-2021, 06:48 PM
Last Post: deanhystad
  How to make this function general to create binary numbers? (many nested for loops) dospina 4 4,394 Jun-24-2020, 04:05 AM
Last Post: deanhystad
  Python beginner - nested while loops mikebarden 1 1,860 Jun-01-2020, 01:04 PM
Last Post: DPaul

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020