Search code examples
pythonhexeffect

Statement seems to have no effect hex to dec converter


When trying to use my hex to decimal converter I get an error for this line where I assign 'E' to 14, it says the statement has no effect

def hex2dec(n):
res = [x for x in n]
for i in range (len(res)):
    if res[i] == 'A' or res[i] == 'a':
        res[i] = 10
    if res[i] == 'B' or res[i] == 'b':
        res[i] = 11
    if res[i] == 'C' or res[i] == 'c':
        res[i] = 12
    if res[i] == 'D' or res[i] == 'd':
        res[i] = 13
    if res[i] == 'E' or res[i] == 'E': ##no effect
        res[i] == 14
    if res[i] == 'F' or res[i] == 'f': 
        res[i] = 15
res2 = [int(x) for x in res]
return res2

Solution

  • This is because you used double equal signs == instead of the single one =. == is used for logical evaluations, while = is used for assigning values to variables. Secondly, you have a typo in the "E" conditional statement. The second condition should be smaller-case "e" instead. Also, please consider using lists or a dictionary instead of 26 if/else statements!