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

问题描述 投票:-2回答:1

[尝试使用十六进制至十进制转换器时,我在将'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'赋给14,它说该语句无效def hex2dec(n):res = [x对于n中的x] i在范围(len(res))...

python hex effect
1个回答
1
投票

这是因为您使用了两个等号==而不是单个等号===用于逻辑评估,而=用于为变量分配值。其次,您在“ E”条件语句中有一个错字。第二个条件应该是小写的“ e”。另外,请考虑使用列表或字典,而不要使用26条if / else语句!

© www.soinside.com 2019 - 2024. All rights reserved.