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: But instead I am getting this: 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: Any help/ suggestion will be very appreciated
|