-2

The task is to have something like this is as the output:

Progress   Trailing   Retriever   Excluded
      *                  *              *                  
                                        *
  • this is a diagram where there are are 4 'headings' and the asterisk represent frequency:
  • I want a way in which I can assign a number say for example for 'progress' and it prints 5 asterisks underneath it and for the rest of the headings:

Here is my attempt:

input:

print("Progress")
x = 0
while x<5:
    print('   *')
    x+=1

output:

Progress
   *
   *
   *
   *
   *

input:

#for next heading 
print("Trailing")
x = 0
while x<2:
    print('   *')
    x+=1

output:

Trailing
   *
   *
  • is there a way I can show them both vertically? just like the intend output or is there another way I can do this where a number is assigned to each header(for the amount of asterisks)

Thanks in advance, I've been trying to find a way for a long time.

the_R
  • 17
  • 5
  • See the `end` argument to the `print` function. – Pranav Hosangadi Oct 14 '20 at 17:14
  • Does this answer your question? [multiple prints on the same line in Python](https://stackoverflow.com/questions/5598181/multiple-prints-on-the-same-line-in-python) – Pranav Hosangadi Oct 14 '20 at 17:15
  • Does this answer your question? [How to print without newline or space?](https://stackoverflow.com/questions/493386/how-to-print-without-newline-or-space) – Anurag Reddy Oct 14 '20 at 17:17
  • So you essentially want to print it a column at a time? There are ways to do that but it is different depending on the terminal so you will need to find a library to allow you to move the terminal cursor like that. I think it would be better to print it out row by row instead to keep things simple. ie `print('Progress Trailing Retriever Excluded')` etc – EDD Oct 14 '20 at 17:23
  • yes a column at a time, could you please explain the row by row? I did this before and the issue was that It would print 4 asterisk on the same row but what if i didn't want an asterisk on a specific row? or a certain number? – the_R Oct 14 '20 at 17:24
  • @the_R so you would print the headings first as a row then add each entry a row at a time printing either `' * '` or `' '` depending on if there should be an asterisk or not. – EDD Oct 14 '20 at 17:26
  • Printing works row by row. You need to lay out your columnar input, but then change your thinking to row-oriented. Then you need to work through your tutorials and documentation on `print` and, if appropriate for your class, loops to work through the data by row. Stack Overflow is not intended as a tutorial resource. – Prune Oct 14 '20 at 17:54

2 Answers2

0

Change

print("Trailing")

To

print("Trailing", end='')

And you will not print the default \n character at the end of the string.

Collin Heist
  • 1,962
  • 1
  • 10
  • 21
  • When I join both the code from progress and trailing and add end after "Trailing", it doesnt align properly rather it prints underneath "progress" . – the_R Oct 14 '20 at 17:23
0

There are ways to move the cursor in a terminal to do it in the way you are probably thinking of but the simplist way is just to print it a line at a time (pseudocode):

print('Progress   Trailing   Retriever   Excluded')
for entry in entries:
    print('  *   ' if entry.progress  else '       ', end='')
    print('  *   ' if entry.trailing  else '       ', end='')
    print('  *   ' if entry.retriever else '       ', end='')
    print('  *   ' if entry.excluded  else '       ') # No end here to add newline
EDD
  • 2,070
  • 1
  • 10
  • 23
  • Sorry could you just provide some more information, I get an error as 'entries' is not defined when i run the code. Thank you for the help, I'm guessing its a class for entry? or? – the_R Oct 14 '20 at 17:37
  • @the_R Im not sure how you are counting the frequency so I just used this for loop instead. Replace it with what you need to iterate over the frequencies you have. replace `entry.progress` with something like `i < progress_freq`. – EDD Oct 14 '20 at 17:40
  • sorry I'm a bit confused could you provide a bit more code please, again thank you for your help. – the_R Oct 14 '20 at 17:50