0
我正在使用jupyter笔记本。我想打印一个简单的整数矩阵的一般形式,即我不能输出是这样的: a [0] [0] = 1 a [0] [1] = 2 a [1] [0 ] = 3 [1] [1] = 4Python中的'print'命令(打印在同一行中)
这是我的程序:
column = int(input("Enter the number of columns: "))
row = int (input("Enter the number of rows: "))
a=[[0 for x in range(column)] for y in range(row)]
for i in range (0, row):
for j in range (0, column):
a[i][j]=int(input(" Enter the elements: "))
for i in range (0, row):
for j in range (0, column):
print ("a[",i,"][",j,"] =",a[i][j],"\t",),
print ("\n")
但是我得到的输出为:
Enter the number of columns: 2
Enter the number of rows: 2
Enter the elements: 1
Enter the elements: 2
Enter the elements: 3
Enter the elements: 4
a[ 0 ][ 0 ] = 1
a[ 0 ][ 1 ] = 2
a[ 1 ][ 0 ] = 3
a[ 1 ][ 1 ] = 4
打印(),函数的循环即使我在后面加了一个逗号,也会换个新的行。请帮助我获得所需的输出格式。谢谢。
是这个Python 2或Python 3? –
@Rawing由于括号不会显示在输出中,我们必须得出结论,它是python 3(或者'print_function'在python 2中使用)。 – Leon
'print()'*函数*(来自Python 3)与Python 2中的'print' *语句*的用法不同。在*函数中*使用'end =“”'参数来抑制换行结束。 – cdarke