我遇到以下while循环时遇到困难。大部分时间它工作正常,但有时它会以某种方式卡住。有人可以查一下代码吗?对我来说似乎很好,但我刚开始使用python。
关于我的客观的一些信息:我需要一个数字序列(0,1)以便稍后创建一个对称矩阵。行的每个总和不得超过3.所以首先我创建第一行,检查总和是否不大于3,然后将这些值放在序列中列的位置(使其对称) 。
mmm = [0]*(len(user_num)*len(user_num))
for r in range(len(user_num)):
mmm[(len(user_num)*r+r+1):(len(user_num)*(r+1))] = row(r,0,1)
while sum(mmm[(r*len(user_num)):((r+1)*len(user_num))]) > 3:
mmm[(len(user_num)*r+r+1):(len(user_num)*(r+1))] = row(r,0,1)
if r != (len(user_num)-1):
for p in range(1,len(user_num)):
if r < p:
if mmm[(len(user_num)*r+p)] == 1:
mmm[(len(user_num)*p+(r))] = 1
else:
mmm[(len(user_num)*p+(r))] = 0