如何仅在 while 循环的最后一次迭代后添加换行符?
How can I add a newline only after the last iteration of a while loop?
这是一个程序,它打印第一个输入整数值并在同一行上打印每十个值,它们之间有一个 space 直到到达最后一个值,我希望它打印最后一个值通过换行符。问题是它没有在最后一个值之后打印新行。
要明确的是,如果打印“第二个整数不能小于第一个”,我不希望打印换行符,如果输入值相等,我也不想要换行符。只有在 int1 < int2 的特殊情况下,我才需要一个换行符,并且只有在最后一次迭代之后。
int1 = int(input())
int2 = int(input())
if int2 < int1:
print('Second integer can\'t be less than the first.')
else:
while int1 <= int2:
print(int1, end = ' ')
int1 += 10
在此先感谢大家提供的任何帮助
在 while 循环结束后简单地打印一个换行符。您会注意到 "after the last iteration" 无论如何都是 "after the finish of"。
你可以使用 numpy 中的 linspace():
import numpy as np
int1 = int(input())
int2 = int(input())
nu = (int2-int1)*10+1
print(*np.linspace(int1,int2,num=nu))
输入:
4
10
输出:
4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 5.0 5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8 5.9 6.0 6.1 6.2 6.3 6.4 6.5 6.6 6.7 6.8 6.9 7.0 7.1 7.2 7.3 7.4 7.5 7.6 7.7 7.8 7.9 8.0 8.1 8.2 8.3 8.4 8.5 8.6 8.7 8.8 8.9 9.0 9.1 9.2 9.3 9.4 9.5 9.6 9.7 9.8 9.9 10.0
我会这样做:
int1 = int(input())
int2 = int(input())
if int1 > int2:
print("Second integer can't be less than the first.")
elif int1 == int2:
print(f"{int1} ", end='')
else:
print(" ".join(map(str, range(int1, int2 + 1, 10))))
我相信这符合您的描述:
int1 = int(input())
int2 = int(input())
if int2 < int1:
print("Second integer can't be less than the first.")
else:
int3 = int1 # don't change int1 as it's needed later
while int3 <= int2:
print(int3, end=' ')
int3 += 10
if int1 < int2:
print()
虽然 int1 == int2
输出不是干净的换行方式,所以它可能不是您想要的。如果你想显示最后一次迭代后的换行打印与 while
循环密切相关,你可以这样做:
while int3 <= int2:
print(int3, end=' ')
int3 += 10
else:
if int1 < int2:
print()
但这既可能造成混淆,也可能造成混淆。
我会这样做。
int1 = int(input())
int2 = int(input())
if int2 < int1:
print('Second integer can\'t be less than the first.')
else:
while int1 <= int2:
print(int1, end = ' ')
int1 += 10
else: #prints a newline when the while loop has completed iteration.
print()
不需要包含任何其他变量或复杂的代码。
这是一个程序,它打印第一个输入整数值并在同一行上打印每十个值,它们之间有一个 space 直到到达最后一个值,我希望它打印最后一个值通过换行符。问题是它没有在最后一个值之后打印新行。 要明确的是,如果打印“第二个整数不能小于第一个”,我不希望打印换行符,如果输入值相等,我也不想要换行符。只有在 int1 < int2 的特殊情况下,我才需要一个换行符,并且只有在最后一次迭代之后。
int1 = int(input())
int2 = int(input())
if int2 < int1:
print('Second integer can\'t be less than the first.')
else:
while int1 <= int2:
print(int1, end = ' ')
int1 += 10
在此先感谢大家提供的任何帮助
在 while 循环结束后简单地打印一个换行符。您会注意到 "after the last iteration" 无论如何都是 "after the finish of"。
你可以使用 numpy 中的 linspace():
import numpy as np
int1 = int(input())
int2 = int(input())
nu = (int2-int1)*10+1
print(*np.linspace(int1,int2,num=nu))
输入:
4
10
输出:
4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 5.0 5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8 5.9 6.0 6.1 6.2 6.3 6.4 6.5 6.6 6.7 6.8 6.9 7.0 7.1 7.2 7.3 7.4 7.5 7.6 7.7 7.8 7.9 8.0 8.1 8.2 8.3 8.4 8.5 8.6 8.7 8.8 8.9 9.0 9.1 9.2 9.3 9.4 9.5 9.6 9.7 9.8 9.9 10.0
我会这样做:
int1 = int(input())
int2 = int(input())
if int1 > int2:
print("Second integer can't be less than the first.")
elif int1 == int2:
print(f"{int1} ", end='')
else:
print(" ".join(map(str, range(int1, int2 + 1, 10))))
我相信这符合您的描述:
int1 = int(input())
int2 = int(input())
if int2 < int1:
print("Second integer can't be less than the first.")
else:
int3 = int1 # don't change int1 as it's needed later
while int3 <= int2:
print(int3, end=' ')
int3 += 10
if int1 < int2:
print()
虽然 int1 == int2
输出不是干净的换行方式,所以它可能不是您想要的。如果你想显示最后一次迭代后的换行打印与 while
循环密切相关,你可以这样做:
while int3 <= int2:
print(int3, end=' ')
int3 += 10
else:
if int1 < int2:
print()
但这既可能造成混淆,也可能造成混淆。
我会这样做。
int1 = int(input())
int2 = int(input())
if int2 < int1:
print('Second integer can\'t be less than the first.')
else:
while int1 <= int2:
print(int1, end = ' ')
int1 += 10
else: #prints a newline when the while loop has completed iteration.
print()
不需要包含任何其他变量或复杂的代码。