Kisze
Spy
- Joined
- Jun 3, 2009
- Messages
- 210
- Reaction score
- 1
So I'm using python in this beginners programming course on uni and I looked through some problems that the students last year had to solve (We might get the same problem later, not sure really) and one of them was to define a function using recursion that given an integer as an argument counts down to zero then back up again. So for example if I write:
>>> bounce(5)
the function bounce should return "5 4 3 2 1 0 1 2 3 4 5"
So I wrote this:
What I can't figure out is how to make it stop after n = -5 if the original input is 5? Or -10 if the original input is 10 etc etc. Right now it just counts down to infinity and beyond. Whether you want to post a solution or just give hints is up to you! I've been pulling my hair trying to figure it out since it's probably really easy and I'm not a even a beginner :/
>>> bounce(5)
the function bounce should return "5 4 3 2 1 0 1 2 3 4 5"
So I wrote this:
Code:
def bounce(n):
print abs(n),
bounce(n-1)
What I can't figure out is how to make it stop after n = -5 if the original input is 5? Or -10 if the original input is 10 etc etc. Right now it just counts down to infinity and beyond. Whether you want to post a solution or just give hints is up to you! I've been pulling my hair trying to figure it out since it's probably really easy and I'm not a even a beginner :/