[英]Python Output Ambiguity
So I have written the following code to generate combinations (n choose k) : 因此,我编写了以下代码来生成组合(n选择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.
我必须说我是python的新手,所以不要对我苛刻。 Thank you in advance.
先感谢您。
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="")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.