Reputation: 1
I need to create the shape below using both a 'for loop' & 'elif statement' for an assignment.
I already have the code to create the shape using 'for loops' only but I'm struggling to change it into a elif statement.
I'm only in my 3rd week of coding so not sure wether this code is concise legible code as I have been piecing it together from various websites all day. I also don't understand why I need to add an elif statement as its seems to complicate the process more?
rows = 6
for i in range(0, rows):
for j in range(0, i + 1):
print("*", end=' ')
print(" ")
for i in range(rows + 1, 0, -1):
for j in range(0, i - 1):
print("*", end=' ')
print(" ")
star pattern this code produces:
*
**
***
****
*****
******
******
*****
****
***
**
*
Upvotes: 0
Views: 2329
Reputation: 1
I know this is coming a bit late and this threat might not be active anymore but you could probably do without most of the variables, couldn't you?
for i in range(1, 10):
if i <= 6:
stars = i
else:
stars = 10 - i
print("* " * stars)
Upvotes: 0
Reputation: 13177
There is a mantra in programming "Do Not Repeat Yourself" aka "DRY". If you look at your current code, both looping blocks look like they do much of the same thing. It might not be a big deal here to duplicate the blocks and make the second one subtly different and honestly when rapidly prototyping I do that kind of thing all the time.
I will then typically go back and refactor/clean up the code to "DRY" it out. Again here it is no big thing, but imagine if the single print("*", end=' ')
in the inner most loops was actually a more complicated set of code and that someone asked for a change. Now you (or the next person) needs to remember that this block of code was repeated.
Of course one way to solve that might be to replace whatever was in the inner loops with a call to a method and thus making the code a little more "DRY". The other way and perhaps what has been suggested to you is that one set of loops might be used to draw both ascending lines and descending lines.
Lets try that using @sembei-norimaki's recommendation.
row_count = 6
start = 1
end = (2 * row_count) + 1
for row_number in range(start, end):
star_count = row_number if row_number <= row_count else end - row_number
for i in range(star_count):
print("*", end=" ")
print()
Note here we are calculating the number of stars to print based on a condition. If our row_number
is less than or equal to the row_count
then we are going to print a number of stars equal to the row_number
. On the other hand once our row_number
exceeds our row_count
we will start reducing the number of starts to print.
This produces:
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
The code here:
star_count = row_number if row_number <= row_count else end -
is your potential if/else
block and is essentially the same as a more traditional:
if row_number <= row_count:
star_count = row_number
else:
star_count = end - row_number
As a final simplification, if you multiply a string in Python you get a repeated string. Thus:
print("* " * 2 == "* * ")
Should give you:
True
Using this we can do away with the inner loop and the extra call to print()
row_count = 6
start = 1
end = (2 * row_count) + 1
for row_number in range(start, end):
star_count = row_number if row_number <= row_count else end - row_number
print("* " * star_count)
Upvotes: 0