In: Computer Science
Python: Write a recursive function that accepts an integer argument, n. The function should display n lines of asterisks on the screen, with the first line showing 1 asterisk, the second line showing 2 asterisks, up to the nth line which shows n asterisks. Test the function.
Code is as follows:
************************************************************************************************
#function to print the asterisks pattern
def pattern(n):
if n>1: #if n is greater than 1
pattern(n-1) #recursive call to n-1
print('*'*n) #print n asterisks
else:
print('*') #else print only one asterisks
#function for testing
def main():
n = int(input("Enter n : ")) #input n
pattern(n) #call function pattern with parameter n
if __name__=="__main__":
main()
***********************************************************************************************
Screenshot of the code:
Output: