Before we start, did you try solving the workbook yourself?
If not, please click the link in the resources folder and follow the instructions for this workbook.
Welcome to Workbook 5.3.
Let us get right into it.
Task one is to ask to choose a number and we need to make sure Tammy can enter the value beside the
question.
I hope this made you think of print, not print line.
If you missed the print versus print line article, I highly recommend you visit it before you look
at this solution.
So here we need to say Hi, Timmy.
Choose a number to count to.
Putting space.
Then we'll say ent number is equal to skin dot next int.
Because we use print instead of print line, it's not going to take up the entire line, which means
next end is going to ask me to enter an integer right beside the following text.
And now task two is to count from zero to that number.
So that should have been a hint for you for I to start at zero.
And it's to count to that number.
So I hope you didn't say I is smaller than number, because if you do that, let's just say he enters
the number five.
By the time I reaches five, this is going to be false and the for loop will not run.
So you want to make sure this is either I is smaller than or equal to number or I is smaller than number
plus one.
I don't know about you, but this seems like the more elegant approach.
Then we'll say I plus plus.
I hope you saw through my little trick there.
And now we simply have to count from zero to the number that they choose.
All we got to do is say print, not print line, because they all have to print on the same line.
Every single time the loop runs, we will print the current number that we're iterating through, plus
some space for the next number to print.
All right.
Let us now visualize the runtime.
Before I let Timmy choose a number to count to let me put a breakpoint right over here.
He is going to count up until five.
All right.
So number equals five I equals zero zero is smaller than or equal to five, which means this loop is
going to run first.
It's going to print the number zero.
And what's beautiful about print as opposed to print line is that print prints, text, but it does
not move to a new line.
So the second time we run this loop, this time I equaling one one is going to print on the exact same
line.
Now I equals two.
Three four.
Five.
And by the time we go back, I a plus plus is going to increase I to equal six.
Six is not smaller than or equal to five, which means the loop is going to break.
And we were able to accomplish our task, which was to count from zero to whatever number Tammy chooses.