在 Python 3 中的一行上打印一个序列
Print a sequence on one line in Python 3
我已经设法使排序正确,但是我不确定如何将它打印在同一行上。我有这个:
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
根据您的第一个(工作)代码判断,您可能正在使用 Python 2. 要使用 print(n, end=" ")
,您首先必须从 Python 导入 print
函数3:
from __future__ import print_function
if n>-6 and n<93:
while (i > n):
print(n, end=" ")
n = n+1
print()
或者,使用旧的 Python 2 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))
您可以像这样使用临时字符串:
if n>-6 and n<93:
temp = ""
while (i > n):
temp = temp + str(n) + " "
n = n+1
print(n)
您可以使用 print 作为函数并指定 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=" ")
这是在 java 中编码的。这两个嵌套循环只是为了打印模式,当我们得到所需的整数序列时,停止变量用于终止循环。
import java.io.*;
import java.util.Scanner;
public class PartOfArray {
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int stop =1; // stop variable
/*nested loops to print pattern */
for(int i= 1 ; i <= n ; i++)
{
for(int j = 1 ; j<=i ; j++)
{
if (stop > n){ break;} //condation when we print the required no of elements
System.out.print(i+ " ");
stop++;
}
}
}
}
我已经设法使排序正确,但是我不确定如何将它打印在同一行上。我有这个:
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
根据您的第一个(工作)代码判断,您可能正在使用 Python 2. 要使用 print(n, end=" ")
,您首先必须从 Python 导入 print
函数3:
from __future__ import print_function
if n>-6 and n<93:
while (i > n):
print(n, end=" ")
n = n+1
print()
或者,使用旧的 Python 2 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))
您可以像这样使用临时字符串:
if n>-6 and n<93:
temp = ""
while (i > n):
temp = temp + str(n) + " "
n = n+1
print(n)
您可以使用 print 作为函数并指定 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=" ")
这是在 java 中编码的。这两个嵌套循环只是为了打印模式,当我们得到所需的整数序列时,停止变量用于终止循环。
import java.io.*;
import java.util.Scanner;
public class PartOfArray {
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int stop =1; // stop variable
/*nested loops to print pattern */
for(int i= 1 ; i <= n ; i++)
{
for(int j = 1 ; j<=i ; j++)
{
if (stop > n){ break;} //condation when we print the required no of elements
System.out.print(i+ " ");
stop++;
}
}
}
}