I am having trouble with the second and third part of this problem. I cannot fig
ID: 3629221 • Letter: I
Question
I am having trouble with the second and third part of this problem. I cannot figure out drawRightTriangle, but figured out drawLeftTriangle. (Using Python 3) Any help would be appreciated!!!!!!
def drawLeftTriangle(height):
"""
Draws a right-angle triangle of height lines with the right angle on the left.
>>> drawLeftTriangle(2)
T
TT
>>> drawLeftTriangle(5)
T
TT
TTT
TTTT
TTTTT
"""
x = " "
for i in range(height):
x = x + 'T'
print(x)
def drawRightTriangle(height):
"""
Draws a right-angle triangle of height lines with the right angle on the right.
>>> drawRightTriangle(2)
T
TT
>>> drawRightTriangle(5)
T
TT
TTT
TTTT
TTTTT
"""
for i in range(height):
def drawTriangle(height):
"""
Draws a right-angle triangle of height lines with the right angle on the right
or on the left, according to the sign of the parameter height.
>>> drawTriangle(5)
T
TT
TTT
TTTT
TTTTT
>>> drawTriangle(-5)
T
TT
TTT
TTTT
TTTTT
"""
Explanation / Answer
The drawRight function has height-1 spaces to the left of the T followed by height-2, height-3, ... to height -height spaces on the final row. Then use the two working methods in the drawTriangle but convert the negative LeftDraw to positive.
def drawRightTriangle(height):
for x in range(1, height+1):
str = ""
for y in range(1,x+1):
str='T'+str
while len(str)< height:
str= " " + str
print(str)
def drawTriangle(height):
if height < 0:
drawRightTriangle(-int(height))
elif height > 0:
drawLeftTriangle(int(height))
else:
print("")
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.