November 5th, 2012, 03:32 PM
Creating equilateral triangles with *'s using print function
An equilateral triangle is one that has all three sides of equal length. Your task for this is to write a program that can print out equilateral triangles of different sizes. You will print them out using a '*' character and the Python "print" function, not with the graphics library.
You should start by asking the user how big of a triangle they want.
To print the triangle, you should break it into lines.
Each line has some space at the start of it, then a number of * characters.
To make the triangles look more balanced, you should put a space between each *.
In order to make the print function not automatically move to the next line, you can pass end="" to it. For example the code print("*", end="") will print a * character. The next time you
print something, it will appear right after the *, not on the next line.
You can use recursion to solve this problem, but you don't have to.
How big? 3
* * *
(periods are there so it doesn't push the *'s to the left when i post)
I currently have a code (shown below) but it's only printing straight lines
x = int(input("Size of triangle:"))
space = " "
star = "*"
x = int(x)
for y in range(1, x + 1,1):
..for z in range(1, x - y + 1,1):
....space = space + ' ' #periods again for format
for z in range(1, 2 * y,1):
..star = star + "*"
print (space + star)