recursion isn't working 2nd time. - python

44 views Asked by At
def twothousand(amt):
    n=500
    div1=amt//n
    mod1=amt%n
    return (mod1,n,div1)


def fivehundred(amt):
    n=200
    div1=amt//n
    mod1=amt%n
    return (mod1,n,div1)


def calculate(amt):
    if amt <10:
        print("hi")

    elif amt>=200 and amt<500:
        mod1,n,div1=fivehundred(amt)
        return (mod1,n,div1)

        #the above return statement isn't returning anything. 
        #That is, now the program doesn't go to the main function 2nd time.

    elif amt>=500 and amt<2000:
        mod1,n,div1=twothousand(amt)
        return (mod1,n,div1)


def main1():
    amt=int(input("Enter the amount: "))
    mod1,n,div1=calculate(amt)
    print (mod1,n,div1)
    #The above print function executes only once.
    if mod1!=0:
        amt=mod1
        calculate(amt)


if __name__=="__main__":
    main1()

OUTPUT:

Enter the amount: 1700
200 500 3

EXPECTED OUTPUT:

Enter the amount: 1700
200 500 3
0 200 1

I'm unable to execute return statement after calculate() function call happens 2nd time, as written in comments. I'm not getting the second output. New to python, kindly help.

sorry for not updating the logic earlier . The logic is:

When user asks for an amount of 1700, he can only be given that amount using 500 and 200 currency. So, the 1st output is - 200 500 3 ; that is, 3 number of 500 currency.. and remaining is 200. I want to call the function calculate till the value mod1 == 0.

1

There are 1 answers

4
Rahul Agarwal On BEST ANSWER

Your main() function should look like this:

def main1():
    amt=int(input("Enter the amount: "))
    mod1,n,div1=calculate(amt)
    print (mod1,n,div1)
    #The above print function executes only once.
    if mod1!=0:
        amt=mod1
        mod1,n,div1 = calculate(amt)
        print (mod1,n,div1)