Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reordering output messages
#1
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?
Reply
#2
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
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
(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.
Reply
#4
(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.
Reply
#5
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
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  logging messages ahead of print messages vindo 6 3,214 Jun-18-2019, 02:45 PM
Last Post: vindo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020