0

Im currently in a begginers python class so dont bash too hard.... but im doing text patterns based on randomly generated numbers and currently am facing an issue I cant figure out

if random_num > 0:
        for j in range(random_num):
            for i in range(random_num):
                print("*"*(random_num-i))
            print()

that is a the part of my code that creates a triangle pattern based off of a random number thats already generated. My results from running this is :

Do you wish to print another pattern (y/n)? y
Random Number:  4
****
***
**
*

****
***
**
*

****
***
**
*

****
***
**
*
````````````````````````````````````````````````````````````````````````````````````````````````````````
it prints the triangle how I want it by taking one off after every row but as you can see it prints itself same amount of times as number generated. anyone have any imput? also I cannot use "break"
ian94
  • 1

2 Answers2

0

You don't need two for loops.

One loop will print n lines, where n is random_number. Each line will have n - i stars, where i is the index of the line--this is because you multiply the asterisk by (random_number-1). So the 0th line will have n - 0 = n stars, then the next will have n - 1 stars, etc.

0

random_num = 4

while random_num > 0:
  print('*'*random_num)
  random_num -= 1

Think of a while loop as an if statement that keeps repeating the loop until the condition is not met.

Fiddle Freak
  • 1,923
  • 5
  • 43
  • 83