How can i modify my code to correctly print the pattern described?
Question:
I want to print this pattern using only while loops. My code works when the height (h) is even but when the height (h) is odd it does not work. Let me know where I need to make changes. The function tapis_2(l,h) must use the other 3 functions and only while loop need to be used.
for example tapis_2(5,5) should print the pattern below:
*#*#*
*#*#*
*#*#*
*#*#*
*#*#*
But when I run my code I get the pattern below:
*#*#
*#*#
*#*#
*#*#
*#*#
def etoile():
print('*',sep='',end='')
def diese() :
print('#', sep='',end='')
def nouvelle_ligne() :
print()
def tapis_2(l,h):
i = 0
while i<l:
j = 0
while j<h//2:
etoile()
diese()
j += 1
nouvelle_ligne()
i += 1
Answers:
Your function should look like:
def tapis_2(l,h):
i = 0
while i<l:
j = 0
while j<h//2:
etoile()
diese()
j += 1
# If h is odd, append a final asterisk to the line
if h % 2 == 1:
etoile()
nouvelle_ligne()
i += 1
I want to print this pattern using only while loops. My code works when the height (h) is even but when the height (h) is odd it does not work. Let me know where I need to make changes. The function tapis_2(l,h) must use the other 3 functions and only while loop need to be used.
for example tapis_2(5,5) should print the pattern below:
*#*#*
*#*#*
*#*#*
*#*#*
*#*#*
But when I run my code I get the pattern below:
*#*#
*#*#
*#*#
*#*#
*#*#
def etoile():
print('*',sep='',end='')
def diese() :
print('#', sep='',end='')
def nouvelle_ligne() :
print()
def tapis_2(l,h):
i = 0
while i<l:
j = 0
while j<h//2:
etoile()
diese()
j += 1
nouvelle_ligne()
i += 1
Your function should look like:
def tapis_2(l,h):
i = 0
while i<l:
j = 0
while j<h//2:
etoile()
diese()
j += 1
# If h is odd, append a final asterisk to the line
if h % 2 == 1:
etoile()
nouvelle_ligne()
i += 1