我有一个python程序,它产生一个三重奏,但在中间,循环之间,附加一个新行。我该如何删除?
源代码:
var = 10
for x in range(var+1):
print(' '*x+'v'*(10-x)*2+' '*x+' '*x+'v'*(10-x)*2)
for p in range(var+1):
print(' '*var+' '*p+'v'*(var-p)*2)
input()
结果:
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
vvvvvvvvvvvvvvvvvv vvvvvvvvvvvvvvvvvv
vvvvvvvvvvvvvvvv vvvvvvvvvvvvvvvv
vvvvvvvvvvvvvv vvvvvvvvvvvvvv
vvvvvvvvvvvv vvvvvvvvvvvv
vvvvvvvvvv vvvvvvvvvv
vvvvvvvv vvvvvvvv
vvvvvv vvvvvv
vvvv vvvv
vv vv
vvvvvvvvvvvvvvvvvvvv
vvvvvvvvvvvvvvvvvv
vvvvvvvvvvvvvvvv
vvvvvvvvvvvvvv
vvvvvvvvvvvv
vvvvvvvvvv
vvvvvvvv
vvvvvv
vvvv
vv
我想删除中间的差距,但.rstrip('\n')
不起作用。
感谢。
答案 0 :(得分:2)
var = 10
for x in range(var):
print(' '*x+'v'*(10-x)*2+' '*x+' '*x+'v'*(10-x)*2)
for p in range(var+1):
print(' '*var+' '*p+'v'*(var-p)*2)
input()
这可以吗?
答案 1 :(得分:2)
这不是换行符。 x使其为10,因此您在for循环中插入一个空行
答案 2 :(得分:2)
如@ValekHalfHeart所述,换行符是在第一个块中创建的
您可以通过更改第一个块进行修复,如下所示:
for x in range(var):
print(' '*x+'v'*(var-x)*2+' '*x+' '*x+'v'*(var-x)*2)
或者如果您想保留(var + 1),请执行以下操作:
for x in range(var+1):
print(' '*x+'v'*(var+1-x)*2+' '*x+' '*x+'v'*(var+1-x)*2)
问题是10 - 10 = 0
在第一个循环的最后一次迭代中,所以你得到一行''字符而没有'v'。
答案 3 :(得分:0)
另一个尖锐的提示
var = 10
for x in range(var):
print(' '*x+'v'+'v'*(var-1-x)*2+' '*(2*x+1)+'v'+'v'*(var-1-x)*2)
for p in range(var):
print(' '*var+' '*p+'v'+'v'*(var-1-p)*2)
...打印
vvvvvvvvvvvvvvvvvvv vvvvvvvvvvvvvvvvvvv
vvvvvvvvvvvvvvvvv vvvvvvvvvvvvvvvvv
vvvvvvvvvvvvvvv vvvvvvvvvvvvvvv
vvvvvvvvvvvvv vvvvvvvvvvvvv
vvvvvvvvvvv vvvvvvvvvvv
vvvvvvvvv vvvvvvvvv
vvvvvvv vvvvvvv
vvvvv vvvvv
vvv vvv
v v
vvvvvvvvvvvvvvvvvvv
vvvvvvvvvvvvvvvvv
vvvvvvvvvvvvvvv
vvvvvvvvvvvvv
vvvvvvvvvvv
vvvvvvvvv
vvvvvvv
vvvvv
vvv
v
表达式可以简化(数学上),但想法是打印:
在上半部分,它加倍,第二部分的缩进也考虑到前一部分。下部有更简单的压痕。