Home > Net >  How to make use of End="" parameter within function
How to make use of End="" parameter within function

Time:10-08

I have been tasked to create a parallelogram using the required function below

def repeatChar(numRepeats, outputChar):
    '''
    output the outputChar numRepeats times on the same line
    '''
    for colNo in range(numRepeats):
        print(outputChar, end='')  # print outputChar and stay on the same line (don't go to next line)  

I have created a working solution but it fails to meet the requirements of the function above due to my lack of understanding how to properly make use of the End parameter So ultimately my question is how would i integrate it into my code below . I have tried substituting the declarations of escape sequence newline "\n" at the end each for loop in my main function for a 'print(outputChar, end="\n")' within my repeatChar function but the output of the parallelogram comes out a bit funky in comparison

Working Solution but does not make proper use of the End parameter

def repeatChar(numRepeats, outputChar):
        output = ""
        for colNo in range(numRepeats):
            output  = outputChar
        return output
   
    
    def main():
        print("This program will output a prallelogram.")
        side = int(input("How long do you want wach side to be? "))
        char = input("Please enter the character you want it to be made of: ")
        output = ""
        
        # loop to output the top triangle
        for topTriangle in range(1, side   1):
            output  = repeatChar(topTriangle, char)   "\n"
    
        # loop to output the bottom triangle
        for btmTriangle in range(1, side   1):
            output  = repeatChar(btmTriangle, " ")   repeatChar((side - btmTriangle), char)   "\n"
        print(output)
    
    
    main()

view sample output here

CodePudding user response:

Consider a function with signature:

def print_line(characters: List[str]):
    """print_line will print the list of characters to console"""

There are trivial solutions here using the standard library, but to follow your exercise using print's end parameter, you could implement this as:

def print_line(characters: List[str]):
    for ch in characters:
        print(ch, end='')
    print()  # output the trailing newline

Consider how you could rework this into your own code, something like:

def repeat_character(n: int, ch: str):
    for _ in range(n):
        print(ch, end='')
    print()

CodePudding user response:

def repeatChar(numRepeats, outputChar, endchar='\n'):
    '''
    output the outputChar numRepeats times on the same line
    '''
    print(outputChar*numRepeats, end=endchar)

If you want it to work properly don't put print in a loop char by char.

CodePudding user response:

Your function repeatChar is useless. Python has this built-in: '@' * 3 => '@@@'. Given that, you don't need to focus on the end parameter at all.

You also don't need the output variable. Print the lines as you go:

print("This program will output a prallelogram.\n")

side = int(input("How long do you want wach side to be? "))
char = input("Please enter the character you want it to be made of: ")

for topTriangle in range(1, side   1):
    print(char * topTriangle)

for btmTriangle in range(1, side   1):
    print(" " * btmTriangle   char * (side - btmTriangle))

This works fine:

This program will output a prallelogram.

How long do you want wach side to be? 10
Please enter the character you want it to be made of: *
*
**
***
****
*****
******
*******
********
*********
**********
 *********
  ********
   *******
    ******
     *****
      ****
       ***
        **
         *
          

But assuming that you had to use that particular repeatChar() function, print empty strings to end the line (print() will output the \n for you):

for topTriangle in range(1, side   1):
    repeatChar(topTriangle, char)
    print('')

for btmTriangle in range(1, side   1):
    repeatChar(btmTriangle, ' ')
    repeatChar(side - btmTriangle, char)
    print('')

You'll notice that this is slower than the char * x approach due to the many individual calls to print().

  • Related