I'm trying to do this triangle in Python as an assignment for my computer science class, but I quite can't figure it out. The output is supposed to be like this:
Select your height. > 5
*
**
***
****
*****
But it comes out like this:
Select your height. > 5
*
**
***
*****
******
Here's the source code.
I apologize for the lengthiness and slight unruliness, I'm currently using vim as my text editor, and I'm fairly new at it. I'm so sorry if this question is bad... I searched for Python's documentation page, and I tried .ljust() and .rjust(), and it doesn't seem to be working for me well. Thanks so much for your help in advance!
# The tools we will use:
# We're just using this to make the program have a more clean, organized feel when executing it.
import time
# This will help build the triangle, along with other variables that will be described later. Spacing is necessary to help build a presentable triangle.
asterisk = "* "
# added will be used in the loop to control how long it will keep repeating the task.
added = 0
# This will multiply the amount of asterisks so that every time a line passes during the loop, it will steadily increase by one.
multiplier = 2
tab = ("""\t\t""")
nextline = ("""\n\n""")
# the finished product!
triangle = ("""""")
# THE PROCESS
print("I will ask you for a height -- any height. Once you tell me, I'll make an isosceles triangle for you.")
#time.sleep(2)
height = input("Please enter a height. > ")
heightNum = int(height)
while heightNum <= 0:
print ("Oops, you can't insert negative numbers and zeroes. Please try another one!")
height = input("Please enter a height. > ")
heightNum = int(height)
while heightNum > added:
if added == 0:
triangle = (tab + triangle + asterisk + nextline)
added += 1
else:
starsline =(tab + asterisk * multiplier + nextline)
triangle = (triangle + starsline)
added += 1
multiplier += 1
print("Here it is! \n\n\n")
print(triangle)
print ("\n\nThis triangle is %d asterisks tall.") % heightNum