python append - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: python append (/thread-4616.html) |
python append - dwiga - Aug-30-2017 I am stuck on my code, that's why I coming here because a lot of python master that solve my question... a = "1,2,3" ax = a.split(",") jo = [] for b in ax: if b == "1": c = "a" elif b == "2": c = "b" elif b == "3": c = "c" jo.append(c) print(jo)and what i get is, but, the only one output that i want is how to fix it?thank you in advance RE: python append - j.crater - Aug-30-2017 Hi, your print statement on line #14 is inside the for loop. That means the line gets executed on each loop run, with intermediate values of "jo". If you only want it to print the final result, place the print statement outside the loop, which will execute it only once - when the loop has finished. RE: python append - ichabod801 - Aug-30-2017 Note that to place it outside the loop, you un-indent it so it has the same indentation as the for statement. RE: python append - dwiga - Aug-31-2017 I did this before a = "1,2,3" ax = a.split(",") jo = [] for b in ax: if b == "1": c = "a" elif b == "2": c = "b" elif b == "3": c = "c" jo.append(c) print(jo)and it return is but, now i get it..thanks all, I get what i want now... a = "1,2,3" ax = a.split(",") jo = [] for b in ax: if b == "1": c = "a" elif b == "2": c = "b" elif b == "3": c = "c" jo.append(c) print(jo)
RE: python append - Sagar - Aug-31-2017 Your print was inside the for loop. RE: python append - dwiga - Sep-04-2017 (Aug-31-2017, 07:50 AM)Sagar Wrote: Your print was inside the for loop. OK, thanks |