Jankat
Jankat

Reputation: 133

Python list zeroth element mix up

I want to write a function decode(chmod) that takes a three digit permissions number as a string and returns the permissions that it grants. It seems to be working unless I ask for the zeroth element. I couldn't figure out why so.

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)

Upvotes: 0

Views: 129

Answers (2)

UltraInstinct
UltraInstinct

Reputation: 44444

(Assuming you want to pass an integer)

043 is base 8 is same as 35 in base 10. You are therefore passing the integer 35 to the decode function.

Try changing the str -> oct

liste = [int(i) for i in oct(chmod)[2:]] #Use [2:] in Py3, and [1:] in Py2

Upvotes: 1

sureshvv
sureshvv

Reputation: 4422

You need quotes around 043. Try '043'.

Upvotes: 1

Related Questions