jeudi 9 avril 2020

Printing a series based on recursion program in Python

I am trying to write a recursive program in Python to print a series. Following is my code:

def pattern(n,k):
    if n > 0:
        print(n, end = ',')
        print(pattern(n-k,k), end=',')
        print(n,end = ',')
    else:
        print(n,end = ',')

pattern(12,5)

The output should be in the form: 12,7,2,-3,2,7,12 i.e. starting with n, and then successively minus 5 till it reaches 0 or negative number and then sums 5 until it reaches 12

But as output I am getting the following: 12,7,2,-3,None,2,None,7,None,12,

Why am I getting the None? How can I remove it and print it as per the condition above?

Aucun commentaire:

Enregistrer un commentaire