print("\n")
Prints a newline, and ends it with a newline. Thus two newlines
Two options to remove one of them:
print()
or
print("\n", end="")
Question
So I have written the following code to generate combinations (n choose k):
#!/usr/bin/env python3
combinations = []
used = []
def init(num):
for i in range(0, num+1):
combinations.append(0)
used.append(0)
def printSolution(coef):
for i in range(1, coef+1):
print(combinations[i], " ", end=' ')
print("\n")
def generateCombinations(which, what, num, coef):
combinations[which] = what
used[what] = 1
if which == coef:
printSolution(coef)
else:
for next in range(what+1, num+1):
if not used[next]:
generateCombinations(which+1, next, num, coef)
used[what] = 0
n = int(input("Give n:"))
k = int(input("Give k:"))
if k <= n:
init(n)
for i in range(1, n+1):
generateCombinations(1, i, n, k)
input()
The problem with it is that when it outputs the text, instead of writing one line after another like this:
Give n:4
Give k:3
1 2 3
1 2 4
1 3 4
2 3 4
It actually outputs it like this:
Give n:4
Give k:3
1 2 3
1 2 4
1 3 4
2 3 4
My question is, why does that actually happen, and how to fix it? I must say that I'm new to python, so don't be harsh on me. Thank you in advance.
Solution
print("\n")
Prints a newline, and ends it with a newline. Thus two newlines
Two options to remove one of them:
print()
or
print("\n", end="")