数学目录E

2024-09-30 06:19:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在开发一个简单的转换器,并将所有转换因子存储在目录中。它接受要从中转换的内容和要转换的内容,检查以确保它是可转换的,并获取需要转换的内容,然后出错并返回此错误:

Traceback (most recent call last):
   File "python", line 20, in <module>
TypeError: string indices must be integers

代码(活着的代码完成后不会永远循环):

alive = 'yes'
while(alive == 'yes'):
        doable = {'pint': ['cup', 'quart', 'galleon', 'pint'], 'quart': ['cup', 'quart', 'pint', 'galleon'], 'galleon': ['cup', 'quart', 'pint', 'galleon'], 'cup': ['cup', 'quart', 'galleon', 'pint', 'tbsp', 'tsp'], 'ounce': ['cup', 'tbsp', 'tsp', 'ounce', 'ml', 'liter', 'pound', 'gram', 'kilogram'], 'tbsp': ['cup', 'tbsp', 'tsp', 'ounce'], 'tsp': ['tsp', 'tbsp', 'cup', 'ounce'], 'ml': ['ml', 'ounce', 'liter'], 'liter': ['ml', 'ounce', 'liter'], 'pound': ['ounce', 'gram', 'kilogram', 'pound'], 'gram': ['ounce', 'gram', 'kilogram', 'pound'], 'kilogram': ['ounce', 'gram', 'kilogram', 'pound']}
        pint = {'cup': 2, 'quart': 0.5, 'galleon': 0.125, 'pint': 1}
        quart = {'cup': 4, 'quart': 1, 'galleon': 0.25, 'pint': 2}
        galleon = {'cup': 16, 'quart': 4, 'galleon': 1, 'pint': 8}
        cup = {'cup': 1, 'quart': 0.25, 'galleon': 0.0625, 'pint': 0.5, 'tbsp': 16, 'tsp': 48, 'ounce': 8}
        ounce = {'cup': 0.125, 'tbsp': 2, 'tsp': 6, 'ounce': 1, 'ml': 30, 'liter': 0.0295735, 'pound': 0.0625, 'gram': 28.3495, 'kilogram': 0.0283495}
        tbsp = {'cup': 0.0616115, 'tbsp': 1, 'tsp': 3, 'ounce': 0.5}
        tsp = {'cup': 0.020833333, 'tbsp': 0.33333, 'tsp': 1, 'ounce': 0.166667}
        ml = {'ml': 1, 'ounce': 0.033814, 'liter': 0.001}
        liter = {'ml': 1000, 'ounce':33.8, 'liter': 1}
        pound = {'ounce': 16, 'gram': 453.592, 'kilogram': 0.453592, 'pound': 1}
        gram = {'ounce': 0.035274, 'gram': 1, 'kilogram': 0.001, 'pound': 0.00220462}
        kilogram = {'ounce': 35.274, 'gram': 1000, 'kilogram': 1, 'pound': 2.2}
        first = input("What would you like to convert from? ")
        second = input("What would you like to convert to? ")
        if(second in doable[first]):
            amount = input("What is the amount of the first measurement? ")
            conversion = first[second]
            answer = float(amount) * float(conversion)
            print("Your answer is: " + str(answer) + " " + second + "(s).")
        else:
            print("\nYOU CAN NOT CONVERT BETWEEN THESE TWO VARIABLES.")
alive = 'no'

你知道为什么会出错,如何修复或者其他方法来避免错误吗?谢谢。你知道吗


Tags: 内容mlfirstcupgramsecondpinttsp
2条回答

conversion = first[second]没有按您所希望的方式工作。first是一个字符串。它不是由那个字符串命名的变量。要获取变量,需要执行eval(first),而eval通常是个坏主意。你知道吗

当您发现自己将变量名作为数据处理时,通常意味着您组织的事情是错误的。与其试图通过名称来获取变量,不如将名称作为字典中的键。你知道吗

在本例中,您已经拥有了相应的字典doable。与其让字典的值是字符串列表(即其他变量的名称),不如将其他字典放在doable中:

doable = { # you might want to change this variable's name
     'pint': {'cup': 2, 'quart': 0.5, 'galleon': 0.125, 'pint': 1},
     'quart': {'cup': 4, 'quart': 1, 'galleon': 0.25, 'pint': 2},
     #...
     }

问题代码现在将变成:

if second in doable[first]: # this works exactly the same as before
    conversion = doable[first][second] # this gets an extra level of indexing
    #...

firstsecond是单位的名称,它们是字符串。 你在那一行要求的是

"ounce"["gram"]

这对运行时系统来说是无稽之谈。字符串“盎司”中没有元素编号“克”。你知道吗

我猜您正试图允许用户输入目录变量的名称。这是Python中功能的严重混乱。字符串值“盎司”与变量“盎司”无关。你知道吗

相反,您必须检查转换是否“可行”。然后需要从适当的目录中获取适当的转换因子。你知道吗


可能的解决方案:

使转换表成为目录目录。您当前的每个目录都将成为此表中的一个主要标题:

conversion = {
    "pint": {'cup': 2, 'quart': 0.5, 'galleon': 0.125, 'pint': 1},
    "quart": {'cup': 4, 'quart': 1, 'galleon': 0.25, 'pint': 2},
    ...
    "kilogram": {'ounce': 35.274, 'gram': 1000, 'kilogram': 1, 'pound': 2.2}

现在,您可以将转换因子引用为

conversion[first][second]

拼写细节:

"galleon" is a ship powered by oars.
"Gallon" is a unit of measurement.

相关问题 更多 >

    热门问题