Error executing program: ZeroDivisionError: decision by zero

Asked 2 years ago, Updated 2 years ago, 225 views

If you run the program below, you will get an error.What's wrong with you?

output results:

Output Results

program:

import numpy as np
import sys
import re
import time
acid=[ ]
score1 = [ ]
e=[ ]
path="C: Users / Hirotaka Adachi / Desktop / profile /"
wr=open(path+"w.txt","w")
'''
defread_fasta(name):
    f = open(name, 'r')
    seq = ' '
    for line in f.readlines():
        ifline[0]!='>':
            seq+=line.strip()
    f.close()
    return seq
'''
###
#X = 'GGGGGUAAAAAAJJJJJJJJJBBB' #human
# Y = 'TGTGTGCTGTCACTC'
default(x_al,x_na):
    time.sleep(1)
    X = x_al
    X_name = x_na
    for acid1 in open(path+"write1.txt","r"):
        aci=re.sub("\n", "", acid1)
        ac=aci.split("\t")
        if X_name==str(ac[0]):
            acid.append(ac[2])
    print(acid)
    for YY in open(path+"ns8_keggID_sample_protain_ID.txt", "r"):
        YY1 = re.sub("\n", "", YY)
        YY2 = YY1.split("\t")
        Y_moto=YY2[0]
        # print(Y_moto)
        Y_name = YY2[1]
        Y = YY2 [2]
        if Y_moto==X_name:
            print(Y_name)
            N=len(X)
            M=len(Y)

            gap_penalty=-2

            H=np.empty(N+1, M+1), dtype='int16')
            L=np.zeros(N+1, M+1), dtype='int8')

            H[0,0] = 0
            for jin range (1, M+1):
                H[0,j] = H[0,j-1] + gap_penalty
                L[0,j] = 0

            for i in range (1, N+1):
                H[i,0] = H[i-1,0] + gap_penalty
                L[i,0] = 2

# Horizontal:0 Diagnostic:1 Vertical:2
            s=np.array([0,0,0], dtype='int')

            for i in range (1, N+1):
                for jin range (1, M+1):
                    s[0] = H[i,j-1] + gap_penalty

                    if X[i-1] == Y[j-1]:
                        score=+1
                    else:
                        score=-1
                    s[1] = H[i-1, j-1] + score

                    s[2] = H[i-1,j] + gap_penalty

                    H[i,j] = np.max(s)
                    L[i,j] = np.argmax(s)
            pairs = [ ]
            i = N
            j = M
            while i!=0 or j!=0:
                if L[i,j] == 0:
                    pairs.append(["-",Y[j-1]] )
                    j = j-1
                elif L[i,j] == 1:
                    pairs.append ([X[i-1], Y[j-1])] )
                    i = i-1
                    j = j-1
                else:
                    pairs.append([X[i-1], "-"])
                    i = i-1

            pairs.reverse()
            i = 1
            acid_s = 0
            acid_all = 0
            acid_p=len(acid)
            For pin pairs:
                ifp[0]!="-":
                    i=i+1
                    for acid_num in acid:
                        if str(i)==acid_num:
                                # print(p)
                            if p[0] == p[1]:
                                acid_s=acid_s+1
            ac_all = acid_s /acid_p
            ac_al=str(ac_all)
                # print(Y_name,ac_all)
            ac_a = [Y_name, ac_al]
            # print(ac_a)
            score1.append(ac_a)
'''
# print ('SCORE=', H[N, M])
For pin pairs:
    print(p[0], end='')
print()

For pin pairs:
    if p[0] == p[1]:
        print('|', end=')
    else:
        print('',end=')
print()

For pin pairs:
    print(p[1], end='')
print()
i = 0
For pin pairs:
    ifp[0]!="-":
        i=i+1
        if i == 12:
            print(p)
'''
if__name__=='__main__':
    wr.write("\t")
    for wwr in open(path+"honyurui.txt"):
        wwr1 = re.sub("\n", "", wwr)
        wr.write(wwr1+"\t")
        e.append(wwr1)
    wr.write("\n")
    for XX in open(path+"hito_tatejiku.txt", "r"):
        score1 = [ ]
        acid=[ ]
        XX1 = re.sub("\n", "", XX)
        XX2 = XX1.split("\t")
        XX_name = XX2[0]
        XX_aliment=XX2[1]
        print(XX_name)
        alimentt(XX_aliment, XX_name)
        print(score1)
        wr.write(XX_name+"\t")
        Foriiine:
            ii_s=-1
            for jjin score1:
                jj1 = jj[0].split(":")
                ifii==jj1[0]:
                    ii_s=jj[1]
            wr.write(str(ii_s)+"\t")
        wr.write("\n")

python

2022-09-30 21:53

1 Answers

You may or may not consider the possibility that the name-related data in the hito_tatejiku.txt tab delimited data may or may not be present in write1.txt.

The line numbers are slightly different, but if you follow the relevant parts from where they occurred, you will see the following:

104: ac_all = acid_s/acid_p#### Error line (0 division)
    ↓
 95—acid_p=len(acid)#### Error Direct Cause The length of the acid list (number of elements) is 0
    ↓
 23: default(x_al,x_na):
 24—time.sleep(1)
 25: X = x_al
 26—X_name = x_na
 27—for acid1 in open (path+"write1.txt", "r"):
 28: aci=re.sub("\n", "", acid1)
 29: ac=aci.split("\t")### Tab delimited "write1.txt"
 30—if X_name==str(ac[0]):#### If the first column matches by name
 31: acid.append(ac[2])#####Add third column of data to the acid list
    ↓
140—for XX in open(path+"hito_tatejiku.txt", "r"):###
141—score1 = [ ]
142—acid=[]#####Initial value is empty list
143:XX1 = re.sub("\n", "", XX)
144:XX2 = XX1.split("\t")### Tab delimited "hito_tatejiku.txt"
145:XX_name = XX2[0]#### The first column is named.
146: XX_aliment=XX2[1]
147—print(XX_name)
148:alimentt(XX_aliment,XX_name)### Call with parameter specification

I don't know the details of the program's purpose or content, but from a broad perspective, I can think of the following measures:

  • After the first for loop (the print(acid) portion), the number of elements is known, so if it is 0, exit the method.
  • It is also meaningful that there were zero corresponding data, so process it to the point where you can add data to score1 in the next for loop and move on to the next loop

Changes to the ending pattern

32:print(acid)
 33: acid_p=len(acid)### Get acid_p(number of acid elements) first
 34—Exit alimentt() if acid_p==0:###0
 35—return
 36:
 37: for YY in open(path+"ns8_keggID_sample_protain_ID.txt", "r"):

Changes to the pattern to continue

32:print(acid)
 33: acid_p=len(acid)### Get acid_p(number of acid elements) first
 34:
 35: for YY in open(path+"ns8_keggID_sample_protain_ID.txt", "r"):
 36: YY1 = re.sub("\n", "", YY)
 37—YY2 = YY1.split("\t")
 38—Y_moto=YY2[0]
 39:#print(Y_moto)
 40—Y_name = YY2[1]
 41: Y = YY2 [2]
 42: if Y_moto==X_name:
 43—print(Y_name)
 44:
 45:#### If acid_p is 0, the following original processing seems to be useless.
 46:#### Add name and number of data '0' (or empty characters) to the next loop
 47—if acid_p == 0:
 48—score1.append([Y_name, '0'])#### or empty string
 49—continue#####Added to this far
 50:
 51: N=len(X)
 52: M=len(Y)


2022-09-30 21:53

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.