python3 rowinput_确保矩阵行的长度都相同(python3)

so I have this python 3 code to input a matrix:

matrix = []

lop=True

while lop:

line = input()

if not line:

lop=False

if matrix != []:

if len(line.split()) != len(matrix[-1]):

print("Not same length")

menu()

values = line.split()

row = [int(value) for value in values]

matrix.append(row)

However,if I enter

1 2 3

4 5 6 7

8 9 0 1 2

my code will let it pass,but you can notice that row 2 and 3 are not same length as row 1; how to prevent that? the row have to be same length as row 1,else it has to return an error message like 'line don't have the same length. I'm not quite sure of how to do that. Maybe:

for row in matrix:

if len(row) == matrix[1]

pass

else:

print('not same length')

But it doesn't work.

Thanks

解决方案

If you want match the first row length, Try this way,

Use len(matrix[0])

for row in matrix:

if len(row) == len(matrix[0]):

pass

else:

print('not same lenght')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值