#๐Ÿ”’ if or try which to use

13 messages ยท Page 1 of 1 (latest)

ebon loom
#

hey

distant thistleBOT
#

@ebon loom

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

ebon loom
#
COLORMAPPING = {
            "black":0,
            "brown":1,
            "red":2,
            "orange":3,
            "yellow":4,
            "green":5,
            "blue":6,
            "violet":7,
            "grey":8,
            "white":9,
        }

TOLERANCE = {
    "grey": 0.05,
    "violet": 0.1,
    "blue": 0.25,
    "green": 0.5,
    "brown": 1,
    "red": 2,
    "gold": 5,
    "silver": 10
}

   
def resistor_label(colors):
    col1 = COLORMAPPING[colors[0]]
    col2 = COLORMAPPING[colors[1]]
    if len(colors) == 5:
       col3 = COLORMAPPING[colors[2]]
       zero_color = COLORMAPPING[colors[3]]
       tolerance = TOLERANCE[colors[4]]
       main_value = int(str(col1)+str(col2)+str(col3))
       print(main_value)
       zeros = 10**zero_color

    elif len(colors) == 4:
        zero_color = COLORMAPPING[colors[2]]
        tolerance = TOLERANCE[colors[3]]
        main_value = int(str(col1)+str(col2))
        zeros = 10**zero_color
    else:
        return "0 ohms"
    

    resistance = main_value*zeros
    print(resistance,"resistance")


    if 10**3<resistance<10**6:
        if resistance % 10**3 == 0:
           resistance //= 10**3
        else:
            resistance /= 10**3
        print(resistance)
        resistance_value = f"{(resistance)} kiloohms ยฑ{tolerance}%"
    elif 10**6<resistance < 10**9:
        if resistance % 10**6 == 0:
            resistance //= 10**6
        else:
            resistance /= 10**6
        resistance_value = f"{(resistance)} megaohms ยฑ{tolerance}%"
    elif 10**9<resistance < 10**12:
        if resistance % 10**12 == 0:
            resistance //= 10**12
        else:
            resistance /= 10**12
        resistance_value = f"{(resistance)} gigaohms ยฑ{tolerance}%"
    else:
        resistance_value = f"{(resistance)} ohms ยฑ{tolerance}%"

    return resistance_value```
#
 col1 = COLORMAPPING[colors[0]]
    col2 = COLORMAPPING[colors[1]]``` at the begining of function  causes the index error if input has less elements so i tried keeping a  else block to give 0ohms but still this raises index error for it my option is to keep the same thing in two if and elif .. or should i use a try except
grizzled pulsar
ebon loom
#

wont it be repetition

grizzled pulsar
#

you should check if the color is "in" COLORMAPPING

#

it depends on what you're trying to do

ebon loom
grizzled pulsar
distant thistleBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.