0

i need to write a program that uses nested loops to draw a certain number of asterisks that decrease by 1 per iteration

so it should print something similar to this, but the starting number of asterisks shouldn't be hardcoded. even if you put in 12 instead of 7, it should still be able to work.

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

right now i have this:

code:

x = ["*", "*", "*", "*"]
print(x)
for i in x[:]:
    if i == "*":
        x.remove(i)
    print(x)

output:

['*', '*', '*', '*']
['*', '*', '*']
['*', '*']
['*']
[]
oh my god
  • 23
  • 1
  • 2
  • 7
  • 1
    Okay, so in your own words, what is different between the output you get, and the output you want? What is your understanding of why this happens? Can you think of a way to solve the problem? – Karl Knechtel Nov 29 '20 at 05:11
  • 1
    See [how to print each item of list](https://stackoverflow.com/questions/15769246/pythonic-way-to-print-list-items).By the way,it's simple to use `str` in your case. – Kahn Nov 29 '20 at 05:13

4 Answers4

0

you can use .join()

x = ["*", "*", "*", "*"]
print(x)
for i in x[:]:
    if i == "*":
        x.remove(i)
    print("".join(x))

or you use the following code:

for i in reversed(range(5)):
    print("*" * i)
0

Here is what you might want if you're looking to write it in nested loops.

number_of_asterisks = 10 # You can change 10 to any number you'd like.

def draw(number_of_asterisks):
    for i in range(number_of_asterisks):
        asterisk = ""
        for _ in range(number_of_asterisks-i):
            asterisk += "*"
        print(asterisk)

draw(number_of_asterisks)
syohey
  • 156
  • 1
  • 1
  • 6
0

You can use this. It is very simple

for y in range(8,0,-1):
    for x in range(y):
        print("*", end="")
    print()
Lakshan Costa
  • 623
  • 7
  • 26
0

The code you have written is not a nested loop, I will give you the code with a nested loop.

for i in range(5+1, 0, -1):
    for j in range(0, i-1):
        print("*", end='')
    print('')
Dr. Toxic
  • 24
  • 4