jeudi 2 novembre 2017

modify if-elif-else statement into a function to print patterns in Python

I wrote a program (1) that prints my name in patterns (each letter in the name is made of its letters that makes a pattern). I am trying to modify it to be a function instead (2). I have read on docs.python.org how to do functions with strings '', but I cannot find enough information to do it correctly. At this point, Python does not print anything at all, not even an error. Just a blank page. Here is my original code that works well and prints RISH (see pix I attached - enter image description here):

1

String = '';
for row in range(0,8):    
    for column in range(0,30):
        if (column == 0 or row == 0 or column == 29):
            String =String + '*'
        elif (column == 2 or ((row == 1 or row == 4) and column > 2 and column < 6) or (column == 6 and row != 1 and row < 4) or (column == row - 1 and row > 3)):  
            String = String + 'R'
        elif (column == 11 or (row == 1 and column > 8 and column <14) or ( row == 7 and column > 8 and column <14 )):  
            String =String+'I'
        elif (((row == 1 or row == 4 or row == 7) and column > 16 and column < 20) or (column == 16 and (row == 2 or row == 3 or row == 7)) or (column == 20 and (row == 1 or row == 5 or row == 6))):  
            String = String + 'S'
        elif (column == 23 or column == 27 or row ==4 and (column>23 and column <27)):
            String = String + 'H'
        else:      
            String = String + ' '    
    String = String + '\n' 

print(String)

What I am trying to achieve is something like that: where if-elif-else code itself is in the function and returns patterns. Could anyone help me understand if I need to define something before the statement in order for it to return the pattern or there is something else has to be written after each if/elif? Because the else statement (String = String + '\n') is very important to print the pattern and I have no clue how to modify it. Grazie! Thanks a bunch!

2

def printName(string = ''):   

for row in range(0,8):    
    for column in range(0,30):
        if (column == 0 or row == 0 or column == 29):
            return '*'
        elif (column == 2 or ((row == 1 or row == 4) and column > 2 and column < 6) or (column == 6 and row != 1 and row < 4) or (column == row - 1 and row > 3)):  
            return 'R'
        elif (column == 11 or (row == 1 and column > 8 and column <14) or ( row == 7 and column > 8 and column <14 )):  
            return 'I'
        elif (((row == 1 or row == 4 or row == 7) and column > 16 and column < 20) or (column == 16 and (row == 2 or row == 3 or row == 7)) or (column == 20 and (row == 1 or row == 5 or row == 6))):  
            return 'S'
        elif (column == 23 or column == 27 or row ==4 and (column>23 and column <27)):
            return 'H'
        else:      
            return ' '    
    String = String + '\n'

printName()

Aucun commentaire:

Enregistrer un commentaire