Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can i modify my code to correctly print the pattern described?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

*#*#
*#*#
*#*#
*#*#
*#*#

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

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading