有没有办法让if条件忽略脚本中的某些行?

时间:2016-12-23 01:54:13

标签: python if-statement exception

有没有办法让if条件忽略某些代码行?这是我的脚本的一部分,它计算有关矩阵的各种事情:

choice=raw_input("What do you wish to do?(sum/sub/mult/scal/det/trace/transp/cofac/adjointa/inv)")


o =raw_input("Enter dimensions of matrix 1(Ej.:3x4, where 3 is the number of rows and for the number of columns)" " ")
r=raw_input("Enter dimensions of matrix 2" " ")
X=[i for i in o]
Y=[i for i in r]
n,m,p,q=int(X[0]),int(X[2]),int(Y[0]),int(Y[2])

rowsofmatrix1,rowsofmatrix2= {},{}

for i in range (1,n+1):
    while True:
        a=raw_input("Enter row " " " +str(i)+ " " " of the first matrix with each row separated with a space" " ")
        M=split(a)
        if len(M)!=m:
            print "should enter a file of" " "+str(m)+" " "numbers"
            continue
        else:
            break
    M=[float(x) for x in M]
    indexcorrection={}

    for h in range(1,m+1):
        indexcorrection[h]=M[h-1]
    rowsofmatrix1[i]=indexcorrection

for j in range (1,p+1):
    while True:
        b=raw_input("Enter row " " " +str(j)+ " " "of the second matrix with each row separated with a space" " ")
        Q=split(b)
        if len(Q)!=q:
            print "should enter a row with" " "+str(q)+" " "numbers"
            continue
        else:
            break
    Q=[float(x) for x in Q]
    indexcorrection={}
    for h in range(1,q+1):
        indexcorrection[h]=Q[h-1]
    rowsofmatrix2[j]=indexcorrection

因此,正如您可能知道的那样,行列式只是一个矩阵的函数,跟踪或反向也是如此。在变化中,添加,减去和乘以矩阵是两个矩阵的函数。因此,当他询问例如行列式时,我不希望询问用户第二矩阵的条目。另外,我不想要求第二个矩阵的维度。 我的问题是:我可以做一个if语句:

if choice!=='sum' and choice!=='mult' and choice!='sub': 
     Ignore the whole block which asks for rows of the second matrix. 
     Ignore line which asks dimension of the second matrix
     Ignore the lines which split dimensions of the second matrix

0 个答案:

没有答案