使用python查找阻力

2024-09-30 08:35:25 发布

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

我编写了一个python代码,使用颜色代码查找阻力

# resistance of the resistor with
# the given color codes
  
# Function to find the resistance 
# using color codes
def findResistance(a, b, c, d):
      
    # Hash-map to store the values 
    # of the color-digits
    color_digit = {'black': '0',
                   'brown': '1', 
                   'red': '2',
                   'orange': '3', 
                   'yellow': '4',
                   'green' : '5', 
                   'blue' : '6',
                   'violet' : '7', 
                   'grey' : '8',
                   'white': '9'}
      
    multiplier = {'black': '1',
                  'brown': '10', 
                  'red': '100', 
                  'orange': '1k', 
                  'yellow': '10k', 
                  'green' : '100k', 
                  'blue' : '1M', 
                  'violet' : '10M', 
                  'grey' : '100M', 
                  'white': '1G'}
      
    tolerance = {'brown': '+/- 1 %', 
                  'red' : '+/- 2 %', 
                 'green': "+/- 0.5 %", 
                  'blue': '+/- 0.25 %', 
                 'violet' : '+/- 0.1 %', 
                  'gold': '+/- 5 %', 
                 'silver' : '+/- 10 %', 
                  'none': '+/-20 %'}
      
    xx = color_digit.get(a)
    yy = color_digit.get(b)
    zz = multiplier.get(c)
    aa = tolerance.get(d)
    print("Resistance = "+xx + yy+
          " x "+zz+" ohms "+aa)
 
          
# Driver Code
if __name__ == "__main__":
    a = input("enter 1st color: ")
    b = input("enter 2nd color: ")
    c = input("enter 3rd color: ")
    d = input("enter 4th color: ")
      


   # Function Call
    findResistance(a, b, c, d)

但当我运行它时,它会给我以下错误

Traceback (most recent call last):
  File "1.py", line 59, in <module>
    findResistance(a, b, c, d)
  File "1.py", line 45, in findResistance
    print("Resistance = "+xx + yy+
TypeError: can only concatenate str (not "NoneType") to str

我的代码有问题吗


Tags: thetoinputgetgreenblueredcolor
2条回答

您不希望使用get,因为如果在字典中找不到键,这将使用默认值(在本例中为None)。但是在这里的用例中,键必须存在,例如pink不能给出有效的输出。一种选择是在findResistance函数中进行普通的字典查找,即

    xx = color_digit[a]
    yy = color_digit[b]
    zz = multiplier[c]
    aa = tolerance[d]

因此,如果给出了无效的输入,那么将引发KeyError。然后,您可以在调用者中对此进行测试(异常的字符串表示形式将是尝试的键):

    try:
        findResistance(a, b, c, d)
    except KeyError as exc:
        print(f'you entered an invalid colour: {exc}')

或者,如果您希望您的函数在这种情况下引发某种不同类型的异常(例如ValueError或某种自定义异常),您可以在函数本身中执行try块,例如:

    try:
        xx = color_digit[a]
        yy = color_digit[b]
        zz = multiplier[c]
        aa = tolerance[d]
    except KeyError as exc:
        raise ValueError(f'invalid colour given: {exc}')

但是允许get提供默认的None,然后在代码的后面得到一个模糊的TypeError是没有用的行为

我修改了关于颜色的电阻计算方法,因为我们需要将颜色系数乘以k值。修改后的代码如下所示:

# resistance of the resistor with
# the given color codes

# Function to find the resistance
# using color codes
def findResistance(a, b, c, d):
    # Hash-map to store the values
    # of the color-digits
    color_digit = {'black': '0',
                   'brown': '1',
                   'red': '2',
                   'orange': '3',
                   'yellow': '4',
                   'green': '5',
                   'blue': '6',
                   'violet': '7',
                   'grey': '8',
                   'white': '9'}

    multiplier = {'black': '0',
                  'brown': '10',
                  'red': '100',
                  'orange': '1000',
                  'yellow': '10000',
                  'green': '100000',
                  'blue': '1000000',
                  'violet': '10000000',
                  'grey': '100000000',
                  'white': '1000000000'}

    tolerance = {'brown': '+/- 1 %',
                 'red': '+/- 2 %',
                 'green': "+/- 0.5 %",
                 'blue': '+/- 0.25 %',
                 'violet': '+/- 0.1 %',
                 'gold': '+/- 5 %',
                 'silver': '+/- 10 %',
                 'none': '+/-20 %'}

    x = color_digit.get(a)
    xval = int(x) * int(multiplier.get(a))
    y = color_digit.get(b)
    yval = int(y) * int(multiplier.get(b))
    z = color_digit.get(c)
    zval = int(z) * int(multiplier.get(c))
    aa = tolerance.get(d)
    print("Resistance = " + str(xval + yval + zval) + " ohms " + " tolerance: %" + aa)


# Driver Code
if __name__ == "__main__":
    a = input("enter 1st color: ")
    b = input("enter 2nd color: ")
    c = input("enter 3rd color: ")
    d = input("enter 4th color: ")

    # Function Call
    findResistance(a, b, c, d)

相关问题 更多 >

    热门问题