![]() |
Reordering output messages - 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: Reordering output messages (/thread-37301.html) |
Reordering output messages - paulo79 - May-25-2022 Hi all, I have a script where I do a loop condition and show ERROR or OK messages depending of each line return. Example: for v_count_gds in cursor_count: v_count_gd=str(v_count_gds[0]) if (v_count_gd != 0): v_cursor_count1 = v_cursor_count1+1 print("WRONG!!") if (v_cursor_count1 < 1) : print("OK ")But as it goes line by line, if first check if fine is print "OK" , if second it not fine it print "WRONG", if third line is ok it print "OK"like below: "OK"" "WRONG" "OK" I want to print all WRONG in first lines. How could I do that? add print outputs to a variable and do a kind of sort and at the end print this variable containing all "print" outputs? RE: Reordering output messages - menator01 - May-25-2022 Might could use this approach odd = [] even = [] for i in range(20): if i % 2 == 0: even.append(str(i)) else: odd.append(str(i)) print(f'Even numbers: {", ".join(even)}') print(f'Odd numbers: {", ".join(odd)}')
RE: Reordering output messages - paulo79 - May-25-2022 (May-25-2022, 04:03 PM)menator01 Wrote: Might could use this approach That worked almos perfectly. I'm just trying to add a blank line between values, it should be like this ? print(f'Even numbers: {", ".join(even)}\n')it is not working. RE: Reordering output messages - Gribouillis - May-25-2022 (May-25-2022, 04:24 PM)paulo79 Wrote: it is not working.f-strings work starting with Python 3.6. Don't use old pythons. RE: Reordering output messages - menator01 - May-25-2022 If you want one value per line, maybe something like odd = [] even = [] for i in range(20): if i % 2 == 0: even.append(str(i)) else: odd.append(str(i)) print('Even Numbers:') [print(line) for line in even] print() print('Odd Numbers:') [print(line) for line in odd]
|