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 ...
*
***
*****
***
*
*
***
*****
***
*
Printing out a triangle using nested for loops
|
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 ...
* *** ***** *** *
Jan-15-2019, 04:34 PM
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
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, 07:04 PM
(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
Jan-15-2019, 07:20 PM
That's why I included:
Quote:Use the two equations show to ...I should have been more specific.
Jan-15-2019, 09:48 PM
(This post was last modified: Jan-15-2019, 09:58 PM by ichabod801.)
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 + 1Result :
Jan-15-2019, 10:01 PM
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: 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
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
Jan-16-2019, 08:28 AM
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 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 |
|