Reputation: 2293
The program should get two numbers from the user. The first number is the amount of triangles. The second number is the amount of rows per triangle.
It's based off a square problem we just did which is
numRows = input('Please enter the number of rows: ')
numRows = eval(numRows)
numAst = 1
for i in range(numRows):
print(numAst*'*')
numAst += 1
I can not for the life of me figure out how to get it to make triangles though. I know I need some sort of outside loop to restart the inner loop, but I'm not sure how to go about that?
Upvotes: 0
Views: 1574
Reputation: 50205
First, it's a little dangerous to use eval
to cast unsafe user input to an integer, so I changed this to int
instead.
Second, you just need to make a nested loop with the number of triangles value to repeat the inner loop X
times. And of course you need to change your inner loop function to print triangles instead.
Try this and see if you can understand it from the explanation above:
numTris = input('Please enter the number of triangles: ')
numTris = int(numTris)
numRows = input('Please enter the number of rows: ')
numRows = int(numRows)
for _ in range(numTris):
for numAst in range(1, numRows + 1):
print(numAst * '*')
print('')
Note: the variable _
is typically used by convention for a value that you don't intend to use. In this case, we only need it to create the loop but don't use it within the loop.
Upvotes: 1