Python Forum
list sum gives unexpected result - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: list sum gives unexpected result (/thread-24206.html)



list sum gives unexpected result - Nesso - Feb-04-2020

I am trying to sum some lists in order to get this result:
Output:
\start5/8 e16 b16 c16 a2 \start3/8 e16 b2 a16 f16 \start6/8 c16 b8 \start6/8 f16 d4 b8 e16 a16 c16 \start2/8 a16 e2 f16 b4 c16 d8 \start4/8 c16 e4 a8 \start4/8 c2 f16 a4 b16 e8
But instead I am getting this:
Output:
\start5/8 \start3/8 \start6/8 \start6/8 \start2/8 \start4/8 \start4/8 e16 b16 c16 a2 e16 b2 a16 f16 c16 b8 f16 d4 b8 e16 a16 c16 a16 e2 f16 b4 c16 d8 c16 e4 a8 c2 f16 a4 b16 e8
My code is :
import random

list=["a","b","c","d","e","f"]
listnumb=["2","4","8","16","16","16","16","16"]
cellular=" "
gofirst= ""

for j in range(0,7):
    random.shuffle(list)
    random.shuffle(listnumb)
    dur=random.randint(2,6)
    gofirst = "\\start" + str(dur) + "/8 "
    print("===============================")

    for i in range(0,dur):
        cellular=cellular+list[i]+listnumb[i%len(listnumb)] + " "

    cellular= gofirst + cellular + "\n"

print(cellular)
Strange thing is that if I change the order on the last operation,
so instead of this: cellular= gofirst + cellular + "\n"

this: cellular = cellular + gofirst + "\n"

It gives me this result that is the same behaviour I would have expected earlier:
Output:
c16 d16 b16 a4 e16 f8 \start6/8 b16 c2 e8 d16 a16 f16 \start6/8 b16 d16 c16 f4 \start4/8 f16 a16 \start2/8 b4 a16 d8 f16 \start4/8 a16 f16 \start2/8 e16 a16 b8 d16 f16 c4 \start6/8
Any help/ suggestion will be very appreciated Heart