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.
python-3.x python-2.7 recursion multiple-return-values
add a comment |
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.
python-3.x python-2.7 recursion multiple-return-values
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 callingcalculate
untilmod1 == 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
add a comment |
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.
python-3.x python-2.7 recursion multiple-return-values
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
python-3.x python-2.7 recursion multiple-return-values
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 callingcalculate
untilmod1 == 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
add a comment |
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 callingcalculate
untilmod1 == 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
add a comment |
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)
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
add a comment |
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)
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
add a comment |
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)
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
add a comment |
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)
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)
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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
untilmod1 == 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