我设法得到了正确的测序,但我不知道如何让它打印在同一行。我有这个:
n = input ("Enter the start number: ")
i = n+7
if n>-6 and n<93:
while (i > n):
print n
n = n+1并尝试过这样的方法:
n = input ("Enter the start number: ")
i = n+7
if n>-6 and n<93:
while (i > n):
print (n, end=" ")
n = n+1发布于 2016-03-24 13:55:45
从您的第一个代码(工作)判断,您可能正在使用Python2。要使用print(n, end=" "),您首先必须从Python3导入print函数:
from __future__ import print_function
if n>-6 and n<93:
while (i > n):
print(n, end=" ")
n = n+1
print()或者,使用旧有的Python2 print语法,在语句之后使用,:
if n>-6 and n<93:
while (i > n):
print n ,
n = n+1
print或者使用" ".join将数字加入到一个字符串中,并一次打印出来:
print " ".join(str(i) for i in range(n, n+7))发布于 2016-03-24 14:01:50
您可以使用范围,使用打印作为函数,并指定sep arg并使用*解压缩。
from __future__ import print_function
n = int(raw_input("Enter the start number: "))
i = n + 7
if -6 < n < 93:
print(*range(n, i ), sep=" ")输出:
Enter the start number: 12
12 13 14 15 16 17 18您还在使用python 2,而不是python 3,否则打印会导致语法错误,所以使用raw_input并强制转换为int。
对于python 3,只需将输入转换为int,并使用相同的逻辑:
n = int(input("Enter the start number: "))
i = n + 7
if -6 < n < 93:
print(*range(n, i ), sep=" ")发布于 2016-03-24 13:56:35
您可以使用如下临时字符串:
if n>-6 and n<93:
temp = ""
while (i > n):
temp = temp + str(n) + " "
n = n+1
print(n)https://stackoverflow.com/questions/36201754
复制相似问题