Python Forum

Full Version: Reordering output messages
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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?
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)}')
Output:
Even numbers: 0, 2, 4, 6, 8, 10, 12, 14, 16, 18 Odd numbers: 1, 3, 5, 7, 9, 11, 13, 15, 17, 19
(May-25-2022, 04:03 PM)menator01 Wrote: [ -> ]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)}')
Output:
Even numbers: 0, 2, 4, 6, 8, 10, 12, 14, 16, 18 Odd numbers: 1, 3, 5, 7, 9, 11, 13, 15, 17, 19

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.
(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.
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]
Output:
Even Numbers: 0 2 4 6 8 10 12 14 16 18 Odd Numbers: 1 3 5 7 9 11 13 15 17 19