Bazfred
Bazfred

Reputation: 67

How to create a right triangle with asterisk without def function?

So far what I have is:

base = int(input("Enter a value"))
for row in range(base):
    for colomb in range(row+1):
        print('*', end='')
        print()

Upvotes: 2

Views: 179

Answers (2)

Adam Smith
Adam Smith

Reputation: 54163

Sharon's answer is the quickest solution to make the code you have work, but you could also do fewer runs through for loops by just printing (once) the entire string. "a" * 3 is "aaa", for instance, so you could do:

for row in range(1, base+1):  # now the range runs [1-base] instead of [0-base-1]
    print("*" * row)

Upvotes: 0

Sharon Dwilif K
Sharon Dwilif K

Reputation: 1228

You were nearly there. You just need to unindent the last print(). Example -

for row in range(base):
    for colomb in range(row+1):
        print('*', end='')
    print()

Upvotes: 2

Related Questions