Python Forum

Full Version: Problem between list and tuple
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello here is my code:

n=int(x[4])
        chiffres = []
        while n > 0:
            chiffres.append(n % 10)
            n = n // 10
            chiffres = tuple(reversed(chiffres))
            pc = []
        for i in range (0,2):
            pc.append(chiffres[i])
        test = ''.join(str(e) for e in pc)
        print(test)
And i get this error

Traceback (most recent call last):
  File "main.py", line 614, in <module>
    chiffres.append(n % 10)
AttributeError: 'tuple' object has no attribute 'append'
[] is not to declare a list? I'm confused

Just to mention, if I add chiffres = list(chiffres) between line 2 and 3 I still get the error
tuples are immutable, thus append is not available.
on line 6, you convert the type chiffres from list to tuple use:
chiffres = chiffres[::-1]
>>> chiffres  = [1,3,6,8]
>>> chiffres
[1, 3, 6, 8]
>>> chiffres = chiffres[::-1]
>>> chiffres
[8, 6, 3, 1]
>>>
Oh yes you're wright! Wrong indentation! Sorry that was a beginner mistake, Thank you ! :)