+ 3
Having A problem with a Python Programme : Pascal's Triangle!
https://code.sololearn.com/c0jQHDtgN0k1/?ref=app Made that programme some time ago. Try and Run it first Plz. Now, If you have, You may have noticed that the programme prints Newlines where I want Spaces, This Bug, I can not remove it, No matter what I try. Plz Help me!
3 Respostas
+ 5
You have to know that the print() function:
- by default insert a new line after each output
- have a named parameter 'end' used to change that default behaviour
So, to fix your problem, you just have to change these commented lines:
for j in range(1, a-i, 1):
#print(" ")
print(" ",end="") # just change default ending char
for k in range(0, i+1, 1):
#print (" ", val)
print (" ", val, end="") # just change default ending char
val=int(val*(i-k)/(k+1))
#print("\n")
print() # no need to print a new line, an empty call will default printe one
+ 2
Thanks Sir
0
You are using print() which by default goes to new line after outputting. There are number of ways to avoid, just google it.
One way is to use following syntax,
print(val, end=" ")
it changes the by deafult newline character to space.
So after every print statement, you'll get a space.
Hope it helps.