I have the below function to get the below output
22
4444
666666
Instead i'm getting
'22\n4444\n666666\n88888888\n'
Any ideas where im going wrong?
def EvenLadder(n):
...: solution = ''
...: if n <= 1:
...: return solution
...: elif n%2 ==0:
...: for i in range(2,n+1,2):
...: solution += (str(i)*i)+"\n"
...: else:
...: n = n - 1
...: for i in range(2,n+1,2):
...: solution += (str(i)*i)+"\n"
...: return solution
Use the addition operator to print a new line after a variable, e.g. print(variable + '\n') . The newline ( \n ) character is a special character in python and is used to insert new lines in a string.
Just use \n ; Python automatically translates that to the proper newline character for your platform.
You can just use n for specifying a newline character, and Python will translate it to the appropriate newline character for that platform.
The new line character in Python is used to mark the end of a line and the beginning of a new line. To create a string containing line breaks, you can use one of the following. Newline code \n(LF), \r\n(CR + LF).
'22\n4444\n666666\n88888888\n'
is the correct string representation of the expected result. In order to actually process the newline characters you need to print it:
print EvenLadder(6)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With