首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python For Loop不介绍?

Python For Loop不介绍?
EN

Stack Overflow用户
提问于 2017-02-20 04:07:23
回答 2查看 74关注 0票数 0

我在试着做一个程序,给出一种化合物的质量,单位是克/摩尔。应该很简单,但是我的一个for循环并没有像它应该的那样运行(对于范围内的x(0,var-1))。它不是遍历从0到var-1的整数,而是将x保持为0。有没有人能帮我看看?非常感谢!

代码如下:

代码语言:javascript
复制
#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)
EN

回答 2

Stack Overflow用户

发布于 2017-02-20 04:13:59

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

此外,range()不包括上限。所以这一行应该是简单的

代码语言:javascript
复制
for x in range(0 , numOfDistElements):
票数 4
EN

Stack Overflow用户

发布于 2017-02-20 04:39:21

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

代码语言:javascript
复制
#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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42332269

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档