0

learning to code here

for i in range(0,5):
    for j in range(0,i+1):
        print("*",end='\t')
    
    print("")

will get you

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

but I want it in reverse

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

how do I get that done?

Nick
  • 138,499
  • 22
  • 57
  • 95
adam
  • 123
  • 1
  • 2
  • 8
  • 4
    Loops can count down as well as up. Surely the book/tutorial that you are following has such examples. – John Coleman Apr 19 '21 at 23:40
  • 2
    In addition to the option of making the outer loop count down instead of up, you could also make the inner loop count up to `5-i` instead of to `i+1`. – Blckknght Apr 19 '21 at 23:41

2 Answers2

1

A pythonic way to accomplish this would be by using list comprehension and library functions.

star_list = ["\t".join(["*" for i in range(j)]) for j in range(5)]
forwards_str = "\n".join(star_str)
backwards_str = "\n".join(reversed(star_str))

print(forwards_str)
print(backwards_str)
Gus
  • 135
  • 6
0

oop count up to 5-i instead of to i+1

worked perfectly

adam
  • 123
  • 1
  • 2
  • 8