Now, we're going to amp up things a little bit more even to show the idea of Nesting, which allows us to build up programs that are much more complicated than the ones we've considered so far. It can help us perform a useful computations. We'll have a good example of that. So, the idea of nesting is that, when we say in a conditional or a loop that you have a sequence of statements as the alternative, anyone of those statements could itself be a conditional or a loop statement. This gives us very complex control flows, but still, we have the ability to try to understand what they're doing because everything is in terms of just conditionals or loops. But definitely, these types of programs are more challenging to debug than the simple ones we've looked at so far. This is just an example of some nesting, and we'll look at this program in much more detail later. So, it's in the middle, that if-else statement is nested within a while loop, and that while loop is nested within a for loop. There's another if statement nested in there. So, that's what nesting is all about. We'll take a look next at how to put together programs using nesting. So, here's a very simple computation. Given an income, calculate the proper tax rate according to this table. Income less than 47,450 should be 22 percent, and the next highest range should be 25 percent and so forth. So, one way to attack this problem is to use nested if, else statement. So, first one says, if income less than 47,450, rate equals 0.22. That's we're supposed to have. Else, then the income's bigger than 47,450. So next, we test whether it's less than 114,650 and set the rate appropriately. Then, else, then we can consider the next case. This is nested if statements to solve this problem. If statement, within an if statement, within an if statement, within an if statement, four levels of nesting. That's a perfectly well-defined way to perform that computation. Now, here's a quick pop quiz. What about this code here? Seems like sort of the same code, but not really. You need those else clauses. Without the else clauses, the initial three if statements don't do anything at all. It's just like running the last one. So, definitely need those else clauses. Now, you notice this time, there's no braces, and it's possible to write chained if-then-else's without braces. But definitely, you want to read in the book about that case because there's potential that there could be ambiguity in what your program says. So, that's just to know we try to avoid a lot of nesting of this sort. If we think there might be ambiguity, we use braces in our code. To illustrate the use of nesting in an actual application, we're going to look at a famous problem known as the Gambler's ruin problem. So, imagine a gambler going off for a weekend to a casino. He starts with a certain amount of money, let's call it stake, and he's going to just go and place $1 fair bets, a 50-50 chance of winning or losing and just keep going. There's two possible outcome. One is that the gambler runs out of money, we'll call that a loss, gambler goes broke. The other outcome, we'll say the gambler sets a goal and the outcome is that he actually reaches the goal before going broke. So, whatever gambler would want to know is, what's the chances of whining and how many bets until the win or loss? That's going to depend on the stake and the goal. So, maybe the gambler would like to know how to set those parameters to fit in all gambling that he wants in the weekend. So, what we're gonna do to study this problem is what's called Monte Carlo simulation. We're just going to simulate the process and keep track of the results. We'll use the simulated coin flipper, already did that with the if statement. We're just going to repeat doing that and just keep track of all the statistics to try to get a handle on these two questions for the gambler, gambler's ruin problem. So, here's the code that gets this job done of simulating the gambler's ruins problem. It's a fair amount of code and it's got this complicated structure of an if within, a while, and a for and so forth. But if we take look at the constituent parts of the code, it's not too difficult to understand what it's doing, and it definitely gets this computational problem done. So, first thing we do is get the parameters from the command line. So, stake is the amount that the gambler starts with. Goal is the amount that the gambler will declare victory and walk away with. Then, we take a third variable, which is trials from the command line. That's the number of times we're going to run the experiment of starting a gambler out with stake doing fair bets until either he runs out of money or hits goal. Usually, we'll run experiment a lot of times. So, this for loop has a variable t, and all t is doing is keeping track of the number of trials that we do. So, say we run a thousand trials, t will go from 0-1,000, and then run the code inside a thousand times, that's our experiment. We have a variable wins that we keep track of the number of times the gambler's wins. Then, what we're going to print out is the number of wins out of how many trials, and then we can use that to figure out our percentage chance of winning. So, that's a for loop. Inside the for loop is just run one experiment. So, we have a variable cash that we start at stake, and inside to run the experiment is a while loop. As long as cash is bigger than zero and less than goal, we flip a coin. So, we flip a coin the same as before. Do a random double between zero and one. If it's less than a half, we win and otherwise, we lose and we decrement cash. Stay within that while loop until either cash is zero or cash is goal. So, if cash equals goal, then we increment wins and keep track of the, of the win. So, one bet is that if statement, if the goals met you count the win, and then we print the number of wins and trials. That's the gambler's ruin simulation code. Not that complicated when you break down the purpose of each one of the statements. So, if we run this for a stake of five, a goal of 25 and we do a thousand trials, we're going to win 191 times out of a thousand, that tells us. So, that's gambler's ruin simulation. Now, actually, this one, this particular problem, it's been known for centuries actually, back to the 17th century. That you can do mathematical analysis to realize to prove that the probability that you're going to win is the stake divided by the goal. So, in our case, with stake of five and a goal of 25, that's a one in five chance of winning or 20 percent. The expected number of bets that you're going to have to make to either win or lose, it's going to be stake times the desired gain. So, let's look at some example. So, this is what the math says, you have a 20 percent chance of turning $500 into $2,500, 525 is 20 percent. But you're going to have to make a million $1 bets to do that. See you want to figure out how long it takes to make a bet? Where do you get this done in a weekend? You only have a 20 percent chance of winning. So, you can adjust your stake and goal to try to figure out what goes on. Now, we can use our program to test these things. So, this is just for five to 25, but it's the same ratio. Yeah, we get about 200 things, and then we can test this one too. Sure enough, it's about 20 percent chance of winning. Now, what's interesting to notice for this little run here, it's the same code, we just ran the parameters higher. We made a billion coin flips there. That's before, we were doing just a few computations with this program, just by typing away bigger parameters. We did a huge number of coin flips and it takes a little time, but not too much on a modern computer. So, the idea of this is that, in this case, what we've done with computer simulation is to validate the mathematical analysis. So, for this problem, the analysis is simpler if you happen to know what the math is. But if you want to devise a more complicated strategy that says that after you double your money, you'll start making $2 bets. Then, if you get within what close of your goal, you make bigger bets or whatever you want. Whatever type of strategy you want, probably, you're not going to be able to do the math, but you could still write the program and do the simulation. So, just digression is that simulation is a very, very powerful tool and can sometimes do things that even a mathematical analysis can't be. The best thing to do might be to do simulation. That's a digression. I think our point for this segment is that you can write a pretty complicated and interesting program with what you know now about loops and conditionals.