Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't figure out how to print * & whitespace vertically instead of hoirzontally

I tried looking at other threads but still couldn't figure it out.

I am using this code:

numbers = [5,1,5,2,4]

for i in numbers:
  for x in range(0,i):
    print "*",
  print""

It prints:

* * * * * 
* 
* * * * * 
* *
* * * *

etc.

I would like it to print:

    *   *
    *   *   *
    *   *   *
    *   * * *
    * * * * *

I understand i must use if statements for whitespace or *

Any help is appreciated

like image 219
Fruit Avatar asked Dec 03 '25 17:12

Fruit


1 Answers

numbers = [5,1,5,2,4]

for h in range(max(numbers), 0, -1):
   for x in numbers:
      if x >= h:
        print '*',
      else:
        print ' ',
   print ""

or a shorter version.

for h in range(max(numbers), 0, -1):
    print ' '.join('*' if x >= h else ' ' for x in numbers) 
like image 121
ryanpattison Avatar answered Dec 06 '25 18:12

ryanpattison



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!