【发布时间】:2017-06-05 10:51:35
【问题描述】:
我正在使用 jupyter 笔记本。我想以一般形式打印一个简单的整数矩阵,即我希望输出如下所示: a[0][0] = 1 a[0][1] = 2 a[1][0] = 3 a[1][1] = 4
这是我的程序:
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
循环中的 print(), 函数会换行,即使我在它后面加了一个逗号。请帮我获得所需的输出格式。谢谢。
【问题讨论】:
-
这是python 2还是python 3?
-
@Rawing 因为括号没有出现在输出中,我们必须断定它是python 3(或
print_function在python 2中使用)。 -
print()函数(来自 Python 3)的使用方式与 Python 2 中的print语句不同。在函数使用end=""参数来抑制换行符结束。 -
非常感谢,现在可以使用了 :)