00:01
Hello, so i'm here to answer this question.
00:05
We are going to make two recursive functions, one for counting down and one for counting up.
00:10
I'm not going to format this exactly since it's a bit vague.
00:16
You can modify my code to get exactly what you want quite easily, so i'm going to focus instead on actually explaining the code, right? so count for yourself which to call for input zero.
00:27
Okay, so let's just jump straight to the code, which i've written for you ahead of time, we've got the appropriate countdown and count up functions.
00:35
They are recursive, and i have actually implemented correct exception handling here.
00:42
So let's start explaining, right? let's just explain countdown, because if you understand the countdown, you can immediately count up, right? and for the purposes of understanding this function, you don't even have to understand this first line here.
00:54
So we provide countdown with an integer n.
00:57
If it's first going to print n and if n is positive it will recurse into itself with n minus 1 so clearly if we give it the number 2 right it will print 2 2 is greater than 0 so it will count down with n minus 1 which is 2 minus 1 1 1 it will print 1 recourse again and now it will be 0 where it will just print itself and it will terminate because it will no longer call itself as this condition fails.
01:33
All this is subject to n being positive because if at any point during this recursion, n is negative, sorry, n is...
01:48
Right, yeah.
01:49
So if n is negative at any point during this recursion, then it will raise a value error and stop the execution of the program, which kind of ties into your question of runtime errors, right? so now that i've explained how countdown works, it's pretty simple to see that we accept some input from the user, right? we cast it into an integer because when you accept user input, it's first a string, right? if that casting fails for any reason, it also results in a value error, which tells you you didn't enter a valid integer.
02:23
You can look this up yourself if you want.
02:27
And if the number is greater than zero, we call countdown.
02:31
If not, we call count up...