recursion isn't working 2nd time. - python











up vote
0
down vote

favorite












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.










share|improve this question
























  • why shud it print 0 200 1 ? there is no logic written in any of the functions that it shud run recursively?
    – Rahul Agarwal
    Nov 22 at 16:12












  • for the 2nd time, it needs to go to fivehundred() function, comeback to the elif loop inside calculate function and return the value. It is doing everything, but the last step isn't happening. That's it's not returning any value to the main.
    – Captain aaloo reportin' in
    Nov 22 at 16:17










  • Your code will break when the value is greater than 10 and less than 200 as their is no "IF" block
    – Rahul Agarwal
    Nov 22 at 16:24










  • There are precisely 0 recursive calls being made in your code. Recursion is defined as when a function calls itself. Not when you repeatedly call a function. What are you trying to do? Do you want to keep calling calculate until mod1 == 0?
    – Matt Messersmith
    Nov 22 at 16:32










  • yes! i want to keep calling calculate until mod1==0. 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
    – Captain aaloo reportin' in
    Nov 22 at 16:36















up vote
0
down vote

favorite












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.










share|improve this question
























  • why shud it print 0 200 1 ? there is no logic written in any of the functions that it shud run recursively?
    – Rahul Agarwal
    Nov 22 at 16:12












  • for the 2nd time, it needs to go to fivehundred() function, comeback to the elif loop inside calculate function and return the value. It is doing everything, but the last step isn't happening. That's it's not returning any value to the main.
    – Captain aaloo reportin' in
    Nov 22 at 16:17










  • Your code will break when the value is greater than 10 and less than 200 as their is no "IF" block
    – Rahul Agarwal
    Nov 22 at 16:24










  • There are precisely 0 recursive calls being made in your code. Recursion is defined as when a function calls itself. Not when you repeatedly call a function. What are you trying to do? Do you want to keep calling calculate until mod1 == 0?
    – Matt Messersmith
    Nov 22 at 16:32










  • yes! i want to keep calling calculate until mod1==0. 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
    – Captain aaloo reportin' in
    Nov 22 at 16:36













up vote
0
down vote

favorite









up vote
0
down vote

favorite











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.










share|improve this question















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.







python-3.x python-2.7 recursion multiple-return-values






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 16:38

























asked Nov 22 at 16:09









Captain aaloo reportin' in

102




102












  • why shud it print 0 200 1 ? there is no logic written in any of the functions that it shud run recursively?
    – Rahul Agarwal
    Nov 22 at 16:12












  • for the 2nd time, it needs to go to fivehundred() function, comeback to the elif loop inside calculate function and return the value. It is doing everything, but the last step isn't happening. That's it's not returning any value to the main.
    – Captain aaloo reportin' in
    Nov 22 at 16:17










  • Your code will break when the value is greater than 10 and less than 200 as their is no "IF" block
    – Rahul Agarwal
    Nov 22 at 16:24










  • There are precisely 0 recursive calls being made in your code. Recursion is defined as when a function calls itself. Not when you repeatedly call a function. What are you trying to do? Do you want to keep calling calculate until mod1 == 0?
    – Matt Messersmith
    Nov 22 at 16:32










  • yes! i want to keep calling calculate until mod1==0. 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
    – Captain aaloo reportin' in
    Nov 22 at 16:36


















  • why shud it print 0 200 1 ? there is no logic written in any of the functions that it shud run recursively?
    – Rahul Agarwal
    Nov 22 at 16:12












  • for the 2nd time, it needs to go to fivehundred() function, comeback to the elif loop inside calculate function and return the value. It is doing everything, but the last step isn't happening. That's it's not returning any value to the main.
    – Captain aaloo reportin' in
    Nov 22 at 16:17










  • Your code will break when the value is greater than 10 and less than 200 as their is no "IF" block
    – Rahul Agarwal
    Nov 22 at 16:24










  • There are precisely 0 recursive calls being made in your code. Recursion is defined as when a function calls itself. Not when you repeatedly call a function. What are you trying to do? Do you want to keep calling calculate until mod1 == 0?
    – Matt Messersmith
    Nov 22 at 16:32










  • yes! i want to keep calling calculate until mod1==0. 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
    – Captain aaloo reportin' in
    Nov 22 at 16:36
















why shud it print 0 200 1 ? there is no logic written in any of the functions that it shud run recursively?
– Rahul Agarwal
Nov 22 at 16:12






why shud it print 0 200 1 ? there is no logic written in any of the functions that it shud run recursively?
– Rahul Agarwal
Nov 22 at 16:12














for the 2nd time, it needs to go to fivehundred() function, comeback to the elif loop inside calculate function and return the value. It is doing everything, but the last step isn't happening. That's it's not returning any value to the main.
– Captain aaloo reportin' in
Nov 22 at 16:17




for the 2nd time, it needs to go to fivehundred() function, comeback to the elif loop inside calculate function and return the value. It is doing everything, but the last step isn't happening. That's it's not returning any value to the main.
– Captain aaloo reportin' in
Nov 22 at 16:17












Your code will break when the value is greater than 10 and less than 200 as their is no "IF" block
– Rahul Agarwal
Nov 22 at 16:24




Your code will break when the value is greater than 10 and less than 200 as their is no "IF" block
– Rahul Agarwal
Nov 22 at 16:24












There are precisely 0 recursive calls being made in your code. Recursion is defined as when a function calls itself. Not when you repeatedly call a function. What are you trying to do? Do you want to keep calling calculate until mod1 == 0?
– Matt Messersmith
Nov 22 at 16:32




There are precisely 0 recursive calls being made in your code. Recursion is defined as when a function calls itself. Not when you repeatedly call a function. What are you trying to do? Do you want to keep calling calculate until mod1 == 0?
– Matt Messersmith
Nov 22 at 16:32












yes! i want to keep calling calculate until mod1==0. 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
– Captain aaloo reportin' in
Nov 22 at 16:36




yes! i want to keep calling calculate until mod1==0. 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
– Captain aaloo reportin' in
Nov 22 at 16:36












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










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)





share|improve this answer























  • still getting the same result. 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 .
    – Captain aaloo reportin' in
    Nov 22 at 16:29










  • It is coming same just like your expected output...edited the answer...please check now!!
    – Rahul Agarwal
    Nov 22 at 16:36










  • Thank you. Got it! Earlier, i added the code in "if" of calculate function. sorry about that.
    – Captain aaloo reportin' in
    Nov 22 at 16:40






  • 1




    I did. But, it says, votes cast by less that 15 rep points are recorded but doesn't change publicly displayed post score. lol This is my 1st time on stackoverflow. I've clicked the accept though. thanks.
    – Captain aaloo reportin' in
    Nov 22 at 16:46











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53434752%2frecursion-isnt-working-2nd-time-python%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










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)





share|improve this answer























  • still getting the same result. 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 .
    – Captain aaloo reportin' in
    Nov 22 at 16:29










  • It is coming same just like your expected output...edited the answer...please check now!!
    – Rahul Agarwal
    Nov 22 at 16:36










  • Thank you. Got it! Earlier, i added the code in "if" of calculate function. sorry about that.
    – Captain aaloo reportin' in
    Nov 22 at 16:40






  • 1




    I did. But, it says, votes cast by less that 15 rep points are recorded but doesn't change publicly displayed post score. lol This is my 1st time on stackoverflow. I've clicked the accept though. thanks.
    – Captain aaloo reportin' in
    Nov 22 at 16:46















up vote
0
down vote



accepted










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)





share|improve this answer























  • still getting the same result. 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 .
    – Captain aaloo reportin' in
    Nov 22 at 16:29










  • It is coming same just like your expected output...edited the answer...please check now!!
    – Rahul Agarwal
    Nov 22 at 16:36










  • Thank you. Got it! Earlier, i added the code in "if" of calculate function. sorry about that.
    – Captain aaloo reportin' in
    Nov 22 at 16:40






  • 1




    I did. But, it says, votes cast by less that 15 rep points are recorded but doesn't change publicly displayed post score. lol This is my 1st time on stackoverflow. I've clicked the accept though. thanks.
    – Captain aaloo reportin' in
    Nov 22 at 16:46













up vote
0
down vote



accepted







up vote
0
down vote



accepted






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)





share|improve this 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)






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 at 16:35

























answered Nov 22 at 16:22









Rahul Agarwal

2,06231026




2,06231026












  • still getting the same result. 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 .
    – Captain aaloo reportin' in
    Nov 22 at 16:29










  • It is coming same just like your expected output...edited the answer...please check now!!
    – Rahul Agarwal
    Nov 22 at 16:36










  • Thank you. Got it! Earlier, i added the code in "if" of calculate function. sorry about that.
    – Captain aaloo reportin' in
    Nov 22 at 16:40






  • 1




    I did. But, it says, votes cast by less that 15 rep points are recorded but doesn't change publicly displayed post score. lol This is my 1st time on stackoverflow. I've clicked the accept though. thanks.
    – Captain aaloo reportin' in
    Nov 22 at 16:46


















  • still getting the same result. 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 .
    – Captain aaloo reportin' in
    Nov 22 at 16:29










  • It is coming same just like your expected output...edited the answer...please check now!!
    – Rahul Agarwal
    Nov 22 at 16:36










  • Thank you. Got it! Earlier, i added the code in "if" of calculate function. sorry about that.
    – Captain aaloo reportin' in
    Nov 22 at 16:40






  • 1




    I did. But, it says, votes cast by less that 15 rep points are recorded but doesn't change publicly displayed post score. lol This is my 1st time on stackoverflow. I've clicked the accept though. thanks.
    – Captain aaloo reportin' in
    Nov 22 at 16:46
















still getting the same result. 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 .
– Captain aaloo reportin' in
Nov 22 at 16:29




still getting the same result. 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 .
– Captain aaloo reportin' in
Nov 22 at 16:29












It is coming same just like your expected output...edited the answer...please check now!!
– Rahul Agarwal
Nov 22 at 16:36




It is coming same just like your expected output...edited the answer...please check now!!
– Rahul Agarwal
Nov 22 at 16:36












Thank you. Got it! Earlier, i added the code in "if" of calculate function. sorry about that.
– Captain aaloo reportin' in
Nov 22 at 16:40




Thank you. Got it! Earlier, i added the code in "if" of calculate function. sorry about that.
– Captain aaloo reportin' in
Nov 22 at 16:40




1




1




I did. But, it says, votes cast by less that 15 rep points are recorded but doesn't change publicly displayed post score. lol This is my 1st time on stackoverflow. I've clicked the accept though. thanks.
– Captain aaloo reportin' in
Nov 22 at 16:46




I did. But, it says, votes cast by less that 15 rep points are recorded but doesn't change publicly displayed post score. lol This is my 1st time on stackoverflow. I've clicked the accept though. thanks.
– Captain aaloo reportin' in
Nov 22 at 16:46


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53434752%2frecursion-isnt-working-2nd-time-python%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

What visual should I use to simply compare current year value vs last year in Power BI desktop

How to ignore python UserWarning in pytest?

Alexandru Averescu