Python For Loop not Incrimenting?

时间:2017-02-19 20:07:23

标签: python

我试图制作一个程序,给出化学化合物每克摩尔质量。应该很简单,但是我的for循环之一不应该像它应该的那样运行(对于范围内的x(0,var-1))。它不是通过从0到var-1的整数运行,而是将x保持为0.有人可以看看吗?非常感谢!

代码如下:

#Set all elements and their mass per mole (up to Xenon)
elements = [['H' , 1.0079] , ['He' , 4.0026] , ['Li' , 6.941] , ['Be' , 9.0122] , ['B' , 10.811] , ['C' , 12.011] , ['N' , 14.007] , ['O' , 15.999] , ['F' , 18.998] , ['Ne' , 20.180] , ['Na' , 22.990] , ['Mg' , 24.305] , ['Al' , 26.982] , ['Si' , 28.086] , ['P' , 30.974] , ['S' , 32.066] , ['Cl' , 35.453] , ['Ar' , 39.948] , ['K' , 39.098] , ['Ca' , 40.078] , ['Sc' , 44.956] , ['Ti' , 47.88] , ['V' , 50.942] , ['Cr' , 51.996] , ['Mn' , 54.938] , ['Fe' , 55.847] , ['Co' , 58.933] , ['Ni' , 58.693] , ['Cu' , 63.546] , ['Zn' , 65.39] , ['Ga' , 69.723] , ['Ge' , 72.61] , ['As' , 74.922] , ['Se' , 78.96] , ['Br' , 79.904] , ['Kr' , 83.80] , ['Rb' , 85.468] , ['Sr' , 87.62] , ['Y' , 88.906] , ['Zr' , 91.224] , ['Nb' , 92.906] , ['Mo' , 95.94] , ['Tc' , 97.907] , ['Ru' , 101.07] , ['Rh' , 102.91] , ['Pd' , 106.42] , ['Ag' , 107.87] , ['Cd' , 112.41] , ['In' , 114.82] , ['Sn' , 118.71] , ['Sb' , 121.76] , ['Te' , 127.60] , ['I' , 126.90] , ['Xe' , 131.29]]

#Start the main loop
while True:
    grams = 0
    numOfDistElements = int(input("How many distinct elements?\n"))

    #Run through all distinct elements in compound
    #x below is not increasing; stays at 0???
    for x in range(0 , numOfDistElements - 1):
        element = str(input("What is the element?\n"))
        numOfIndivElements = int(input("How many?\n"))

        #Check if enterred element is a valid element
        for y in elements:
            if y[1] == element:

                #Add that element's weight to total mass
                grams += y[2] * numOfIndivElements

            print(grams)

2 个答案:

答案 0 :(得分:4)

从0开始对数组编制索引。将y[1]y[2]分别更改为y[0]y[1]

此外,range()不包括上限。所以这条线应该只是

for x in range(0 , numOfDistElements):

答案 1 :(得分:0)

我建议使用字典而不是列表。此外,您的目标可以通过一些逻辑来完成,而不是让用户告诉您有多少元素。

#Set all elements and their mass per mole (up to Xenon)
elements = [['H' , 1.0079] , ['He' , 4.0026] , ['Li' , 6.941] , ['Be' , 9.0122] , ['B' , 10.811] , ['C' , 12.011] , ['N' , 14.007] , ['O' , 15.999] , ['F' , 18.998] , ['Ne' , 20.180] , ['Na' , 22.990] , ['Mg' , 24.305] , ['Al' , 26.982] , ['Si' , 28.086] , ['P' , 30.974] , ['S' , 32.066] , ['Cl' , 35.453] , ['Ar' , 39.948] , ['K' , 39.098] , ['Ca' , 40.078] , ['Sc' , 44.956] , ['Ti' , 47.88] , ['V' , 50.942] , ['Cr' , 51.996] , ['Mn' , 54.938] , ['Fe' , 55.847] , ['Co' , 58.933] , ['Ni' , 58.693] , ['Cu' , 63.546] , ['Zn' , 65.39] , ['Ga' , 69.723] , ['Ge' , 72.61] , ['As' , 74.922] , ['Se' , 78.96] , ['Br' , 79.904] , ['Kr' , 83.80] , ['Rb' , 85.468] , ['Sr' , 87.62] , ['Y' , 88.906] , ['Zr' , 91.224] , ['Nb' , 92.906] , ['Mo' , 95.94] , ['Tc' , 97.907] , ['Ru' , 101.07] , ['Rh' , 102.91] , ['Pd' , 106.42] , ['Ag' , 107.87] , ['Cd' , 112.41] , ['In' , 114.82] , ['Sn' , 118.71] , ['Sb' , 121.76] , ['Te' , 127.60] , ['I' , 126.90] , ['Xe' , 131.29]]
dict = {}
for item in elements:
    dict[item[0]] = item[1]

string = input("Enter the compound ")
i = 0
grams = 0
while i<len(string):
    one_letter = string[i]
    two_letters = string[i:i+2]

    #check two letters first, since He should be recognized before H
    if two_letters in dict:
        #check for number following element, as in H2O
        if i+2<len(string):
            if string[i+2].isdigit(): #digit follows element
                grams += dict[two_letters]*eval(string[i+2])
                i +=3
                continue
            else: #No digit, element is a single
                grams += dict[two_letters]
                i += 2
                continue
        else: #no room for a digit after the element; string isn't long enough
            grams += dict[two_letters]
            i += 2
            continue

    elif one_letter in dict:
        if i+1<len(string):
            if string[i+1].isdigit(): #digit follows element
                grams += dict[one_letter]*eval(string[i+1])
                i +=2
                continue
            else:
                grams += dict[one_letter]
                i += 1
                continue
        else: #no room for a digit after the element; string isn't long enough
            grams += dict[one_letter]
            i += 1
            continue
    else:
        print("Invalid compound")
        break

print(grams)