我想写一个函数解码(chmod),它将一个三位数的权限编号作为一个字符串并返回它授予的权限。 它似乎工作,除非我要求第零个元素。我无法弄清楚为什么如此。Python列表第零个元素混合
def decode(chmod):
liste = [int(i)for i in str(chmod)]
chmod = ["None", "Execute only", "Write only",
"Write and Execute", "Read only",
"Read and Execute", "Read and Write",
"Read Write and Execute"]
for i in liste:
print chmod[i]
return liste
print decode(043)
感谢您的答复。但我不应该参与投入。我只是测试它是否有效。 – janvar
当python看到043时,它认为它是一个八进制格式的整数。你的解码函数会得到这个intger。对str()做的不是你想要的。如果你想传递八进制整数,你应该将它转换回八进制字符串表示。 (str()不够)。 – sureshvv
@grv:你写的描述是这样的:“将三位数的权限号**作为字符串**”。但在你的测试中,你没有传递一个字符串;你传递一个整数。因此需要报价。 –