数组中未存储某些数据

时间:2019-08-31 21:28:21

标签: python python-3.x

所以我正在研究将化学方程式分解为化合物,然后将其分解为元素和原子数的代码。但是,由于某种原因,每种化合物的最后一个元素都没有存储在数组中。

class compound(object):  # A class of compounds. It stores all the relevant data for the compound
    def __init__(self, n_compound):
        self.n_compound = n_compound
        self.f_compound = str(n_compound)
        temp = ""
        e = ""
        v = "0"
        self.element = []
        self.val = []
        for i in range(0, len(self.f_compound)-1):
            if self.f_compound[i].isalpha() and self.f_compound[i+1].isupper():
                temp += self.f_compound[i] + "1"
            else:
                temp += self.f_compound[i]
        temp += self.f_compound[len(self.f_compound) - 1]
        if temp[len(temp)-1].isalpha():
            temp += "1"
        self.f_compound = temp
        ########issue is appearing in the code here
        for i in range(0, len(self.f_compound)):
            if self.f_compound[i].isalpha():
                if v != "0":
                    self.element.append(str(e))
                    self.val.append(int(v))
                    e = self.f_compound[i]
                    v = "0"
                    i -= 1
                else:
                    e += self.f_compound[i]
            elif self.f_compound[i].isdigit():
                v += self.f_compound[i]
        ########This block of code Is not working properly 
        print(self.n_compound)
        for x in range(0, len(self.element)):
            print(self.element[x] + ": " + str(self.val[x]))
        print(".............................................")



equation = str('Cu + HNO3 -> CuN2O6 + H2O + NO')
#str(input("Enter chemical equation: "))
equation = equation.replace("->", "+").replace(' ', '')
compounds = []  # An array of compounds

for i in range(0, len(equation.split("+"))):
     # Assigning a compound name to an object of the compound class and storing it in the array
    compounds.append(compound(equation.split("+")[i]))

我得到以下输出:


.............................................
HNO3
H:1
N:1
...................................................
CuN2O6
铜:1
N:2
...................................................

高:2
...................................................

N:1
...................................................
< / p>

但是,正确的输出应为:


铜:1
.............................................
HNO3
H:1
N:1
O:3
...................................................
CuN2O6
铜:1
N:2
O:6
...................................................

高:2
O:1
...................................................

N:1
O:1
...................................................
< / p>

1 个答案:

答案 0 :(得分:0)

如果未显示最后一个元素,则意味着您必须查看for循环是否将其排除在外,或者仅在最后一次迭代后不处理结果。

在这种情况下,我认为您只需在循环结束后附加最后一个元素及其价:

for i in range(0, len(self.f_compound)):
    if self.f_compound[i].isalpha():
        if v != "0":
            self.element.append(str(e))
            self.val.append(int(v))
            e = self.f_compound[i]
            v = "0"
            i -= 1
        else:
            e += self.f_compound[i]
    elif self.f_compound[i].isdigit():
        v += self.f_compound[i]
# Fix :
self.element.append(str(e))     
self.val.append(int(v))     

结果:

Cu
Cu: 1
.............................................
HNO3
H: 1
N: 1
O: 3
.............................................
CuN2O6
Cu: 1
N: 2
O: 6
.............................................
H2O
H: 2
O: 1
.............................................
NO
N: 1
O: 1
.............................................