在python中创建一个带有用户输入数字的3x3矩阵

时间:2014-12-06 15:45:22

标签: python python-3.x

我正在尝试在python中创建一个3 * 3矩阵,但我需要帮助用户输入数字。我希望有人可以帮助我,谢谢你。

a = [],[],[] b = [],[],[] c = [],[],[] 
Matrix A = [a,b,c] 
while True: 
   number=input("Please Enter Elements of Matrix A:") 
   [range(3) for i in range(3)]

1 个答案:

答案 0 :(得分:2)

您可以使用嵌套列表解析创建数组,然后您不需要while循环只需使用嵌套for,如果您需要,还需要将输入转换为int将数字存储为int:

>>> A=[[[] for i in range(3)] for i in range(3)]
>>> for i in range(3):
...   for j in range(3):
...     number=int(input("Please Enter Elements of Matrix A:")) 
...     A[i][j]=number

演示:

Please Enter Elements of Matrix A:1
Please Enter Elements of Matrix A:2
Please Enter Elements of Matrix A:3
Please Enter Elements of Matrix A:3
Please Enter Elements of Matrix A:4
Please Enter Elements of Matrix A:4
Please Enter Elements of Matrix A:5
Please Enter Elements of Matrix A:6
Please Enter Elements of Matrix A:7
>>> A
[[1, 2, 3], [3, 4, 4], [5, 6, 7]]