简体   繁体   中英

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:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM