Python Recursion: Assume the availability of a function named printStars that ca
ID: 3692763 • Letter: P
Question
Python Recursion: Assume the availability of a function named printStars that can be passed a parameter containing a non-negative integer value. The function prints out the given number of asterisks. Write a function named printTriangle that receives a parameter that holds a non-negative integer value and prints a triangle of asterisks as follows: first a line of n asterisks, followed by a line of n-1 askterisks, and then a line of n-2 asterisks, and so on. The function must not use a loop of any kind (for, while, do-while) to accomplish its job. The function should invoke printStars to accomplish the task of printing a single line.
Explanation / Answer
def printStars(n):
if n>0
print '*'
printStars(n-1)
endif
end
def printTriangle(n):
if n>0
printStars(n)
printTriangle(n-1)
endif
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.