Unwanted blank line in python output
Question:
For class I am supposed to write a code that output asterisk in the form of a number 8. I don’t understand what is wrong with my code and it adds a new line at the start, but in this challenge.
So, the expected output is supposed to look like this:
*****
* *
*****
* *
*****
While my code looks like this:
for i in range(0,5):
print()
for j in range(0,5):
if i%2==0:
print("*",end='')
else:
if j==0 or j==4:
print("*",end='')
else:
print(" ",end='')
but my output adds a new line at the start( so it looks like the expected output with an extra new line) then prints the rest of the code as intended.
please help.
Answers:
You are starting with a blank line, and shouldn’t
You could change this line:
print()
to:
if i>0:
print()
So that the code, with correct indenting, becomes:
for i in range(0,5):
if i>0:
print()
for j in range(0,5):
if i%2==0:
print("*",end='')
elif j==0 or j==4:
print("*",end='')
else:
print(" ",end='')
Alternatively, you could move the printing of ()
to the end of the for i
loop, so that it would no longer need to be in an if
. That is a little more conventional, since it finishes the output with a newline.
for i in range(0,5):
for j in range(0,5):
if i%2==0:
print("*",end='')
elif j==0 or j==4:
print("*",end='')
else:
print(" ",end='')
print() # <----- No `if`, if you place this `print()` here instead.
The print()
call should be at the bottom of the loop, not the top, so you don’t print a blank line at the beginning.
for i in range(0,5):
for j in range(0,5):
if i%2==0:
print("*",end='')
else:
if j==0 or j==4:
print("*",end='')
else:
print(" ",end='')
print()
You can also do it without the nested loop, since there are just two strings to print, they don’t need to be created dynamically.
for i in range(5):
if i % 2 == 0:
print("*****")
else:
print("* *")
For class I am supposed to write a code that output asterisk in the form of a number 8. I don’t understand what is wrong with my code and it adds a new line at the start, but in this challenge.
So, the expected output is supposed to look like this:
*****
* *
*****
* *
*****
While my code looks like this:
for i in range(0,5):
print()
for j in range(0,5):
if i%2==0:
print("*",end='')
else:
if j==0 or j==4:
print("*",end='')
else:
print(" ",end='')
but my output adds a new line at the start( so it looks like the expected output with an extra new line) then prints the rest of the code as intended.
please help.
You are starting with a blank line, and shouldn’t
You could change this line:
print()
to:
if i>0:
print()
So that the code, with correct indenting, becomes:
for i in range(0,5):
if i>0:
print()
for j in range(0,5):
if i%2==0:
print("*",end='')
elif j==0 or j==4:
print("*",end='')
else:
print(" ",end='')
Alternatively, you could move the printing of ()
to the end of the for i
loop, so that it would no longer need to be in an if
. That is a little more conventional, since it finishes the output with a newline.
for i in range(0,5):
for j in range(0,5):
if i%2==0:
print("*",end='')
elif j==0 or j==4:
print("*",end='')
else:
print(" ",end='')
print() # <----- No `if`, if you place this `print()` here instead.
The print()
call should be at the bottom of the loop, not the top, so you don’t print a blank line at the beginning.
for i in range(0,5):
for j in range(0,5):
if i%2==0:
print("*",end='')
else:
if j==0 or j==4:
print("*",end='')
else:
print(" ",end='')
print()
You can also do it without the nested loop, since there are just two strings to print, they don’t need to be created dynamically.
for i in range(5):
if i % 2 == 0:
print("*****")
else:
print("* *")