简体   繁体   English

声明似乎对十六进制到十进制转换器没有影响

[英]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当尝试使用我的十六进制到十进制转换器时,我将“E”分配给 14 的这一行出现错误,它表示该语句无效

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

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.其次,您在“E”条件语句中有一个错字。 The second condition should be smaller-case "e" instead.第二个条件应该是小写的“e”。 Also, please consider using lists or a dictionary instead of 26 if/else statements!另外,请考虑使用列表或字典而不是 26 个 if/else 语句!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM