声明似乎对十六进制到十进制转换器没有影响
Statement seems to have no effect hex to dec converter
当尝试使用我的十六进制到十进制转换器时,我在将 '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
这是因为您使用了两个等号 ==
而不是单个等号 =
。 ==
用于逻辑评估,而 =
用于为变量赋值。其次,您在 "E" 条件语句中有错字。第二个条件应该是 smaller-case "e" 。另外,请考虑使用列表或字典而不是 26 if/else 语句!
当尝试使用我的十六进制到十进制转换器时,我在将 '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
这是因为您使用了两个等号 ==
而不是单个等号 =
。 ==
用于逻辑评估,而 =
用于为变量赋值。其次,您在 "E" 条件语句中有错字。第二个条件应该是 smaller-case "e" 。另外,请考虑使用列表或字典而不是 26 if/else 语句!