1 00:00:00,690 --> 00:00:03,420 At this point, we've created two while loops. 2 00:00:03,540 --> 00:00:08,070 The first while loop is for the user so that they can continue taking cards 3 00:00:08,370 --> 00:00:12,660 and the second while loop is for the computer so that the dealer can follow 4 00:00:12,660 --> 00:00:16,410 their strategy. Now it's time to tackle hint 13 5 00:00:16,470 --> 00:00:18,870 where we create a function called compare. 6 00:00:20,220 --> 00:00:23,520 We pass in the final user_score and computer_score, 7 00:00:24,180 --> 00:00:28,380 and then we compare the scores to see who has won, who has lost, 8 00:00:28,560 --> 00:00:30,330 or if they have a draw. 9 00:00:31,740 --> 00:00:34,080 So remember that in order to call a function, 10 00:00:34,470 --> 00:00:37,710 it has to be declared before the line we want to use it. 11 00:00:38,160 --> 00:00:41,640 So let's go back up and after hint number six 12 00:00:41,640 --> 00:00:43,560 where we had the last function, 13 00:00:43,890 --> 00:00:47,880 let's add this hint here and create our compare function here. 14 00:00:48,600 --> 00:00:53,600 This compare function is going to take the user_score and also the computer_ 15 00:00:54,060 --> 00:00:57,420 score and it's going to compare these two scores. 16 00:00:58,080 --> 00:01:02,040 If the user_score is equal to the computer_score, 17 00:01:02,790 --> 00:01:06,390 then we said this should mean that it's a draw. 18 00:01:07,170 --> 00:01:11,430 So we'll return some sort of message that represents this. 19 00:01:12,090 --> 00:01:17,090 But elif or else if the computer has a Blackjack, 20 00:01:19,140 --> 00:01:23,610 so the computer_score is equal to zero, well in this case 21 00:01:23,640 --> 00:01:28,640 we should return that the user has lost and the opponent has a Blackjack. 22 00:01:29,460 --> 00:01:32,940 And then we go through this list and we add in all of these other conditions. 23 00:01:33,090 --> 00:01:34,260 So I'm gonna speed it up a bit. 24 00:01:37,440 --> 00:01:39,090 Now for the final condition, 25 00:01:39,780 --> 00:01:43,110 it says that the player with the highest score should win. 26 00:01:43,680 --> 00:01:48,680 So we can write an elif that compares the user_score against the computer score 27 00:01:50,910 --> 00:01:55,890 and we're basically saying if the user_score is greater than the computer score, 28 00:01:56,160 --> 00:01:58,800 well, then that means that the user wins. 29 00:02:00,420 --> 00:02:05,250 But if that's not true and because we've covered everything else, 30 00:02:05,640 --> 00:02:09,090 we now have the final return, which is you lose. 31 00:02:11,700 --> 00:02:12,030 Now, 32 00:02:12,030 --> 00:02:17,030 remember that if, elif, else statements are evaluated from top to bottom. 33 00:02:18,570 --> 00:02:22,800 Even though if the computer score is a zero which is a Blackjack 34 00:02:23,160 --> 00:02:26,490 which is obviously much less than whatever score the user might have, 35 00:02:27,060 --> 00:02:32,060 because it's going to return at this point and it's going to exit the function, 36 00:02:32,700 --> 00:02:37,020 it will never get to this comparison if this is already true. 37 00:02:37,620 --> 00:02:40,770 If you get confused about how if elif and else works 38 00:02:41,130 --> 00:02:44,580 especially the order in which you add the conditions, 39 00:02:44,910 --> 00:02:48,630 have a look back at the lesson where we talked about this in more detail. 40 00:02:49,050 --> 00:02:51,660 This might be a good time to revise that concept. 41 00:02:52,380 --> 00:02:55,440 But now that we've completed our compare function, 42 00:02:55,560 --> 00:02:58,830 we're going to use it at the end of our code here 43 00:02:59,290 --> 00:03:02,440 because this is a time that the user is done drawing cards, 44 00:03:02,500 --> 00:03:04,210 the computer's done drawing cards 45 00:03:04,690 --> 00:03:07,660 and now we can call that compare function 46 00:03:09,370 --> 00:03:14,370 and pass in the user_score and also the computer_score. 47 00:03:16,060 --> 00:03:20,080 So this should hopefully give us the final result 48 00:03:20,290 --> 00:03:24,550 as a piece of text. Let's run the code as it is and see what happens. 49 00:03:25,120 --> 00:03:29,020 We started out with a 3 and a 6 making a 9. 50 00:03:29,740 --> 00:03:34,000 Now we know the computer at least got a 10, so let's add another card. 51 00:03:34,690 --> 00:03:38,050 So now we have a score of 11 with 3, 6, and 2, 52 00:03:38,380 --> 00:03:43,330 so we can still keep going. And luckily we've managed to get a total of 21. 53 00:03:43,870 --> 00:03:48,310 Now we're pretty much invincible I think. So if I go ahead and hit no 54 00:03:48,310 --> 00:03:52,360 cause I don't want to take any more cards, then let's see what the result is. 55 00:03:52,930 --> 00:03:56,230 So it tells us we win and a big smiley face. 56 00:03:56,620 --> 00:03:59,590 So we can probably give the user a little bit more information, 57 00:03:59,650 --> 00:04:04,630 like revealing the final deck; what the computer got, and what we got. 58 00:04:05,350 --> 00:04:09,070 I'm going to go ahead and create some other print statements above this one. 59 00:04:09,910 --> 00:04:14,910 Maybe I'll tell the user that your final hand is this, 60 00:04:17,320 --> 00:04:20,290 and the final score was... 61 00:04:21,519 --> 00:04:24,700 And then let's reveal to them the computer_score, 62 00:04:27,190 --> 00:04:31,930 and then finally we can print the user_score and the computer_score to 63 00:04:31,930 --> 00:04:35,110 tell them who won, who lost, or whether if its a draw. 64 00:04:37,510 --> 00:04:41,380 In this deck, we've got 14, so let's add another card to it. 65 00:04:41,740 --> 00:04:45,040 Unfortunately we went over because we've got another 10. 66 00:04:45,340 --> 00:04:48,970 So our final hand is 10, 4, 10 67 00:04:49,480 --> 00:04:54,220 and the final score was 24. The computer's final hand was 21, 68 00:04:54,250 --> 00:04:55,083 lucky them. 69 00:04:55,150 --> 00:05:00,150 So we went over and we lost. If we wanted to play another game because we are 70 00:05:01,300 --> 00:05:02,470 Blackjack addicts, 71 00:05:02,740 --> 00:05:06,160 then we want to give the user some sort of means of doing this right? 72 00:05:06,730 --> 00:05:09,430 And that's what the last hint addresses. 73 00:05:09,640 --> 00:05:12,790 So we're going to ask the user if they want to restart the game. 74 00:05:13,120 --> 00:05:14,230 If the answer is yes, 75 00:05:14,290 --> 00:05:18,580 then we're going to clear the console and start a new game of Blackjack and also 76 00:05:18,760 --> 00:05:21,850 show the logo from art.py while we're at it. 77 00:05:23,290 --> 00:05:27,580 Let's go ahead and add a input and we can ask them, 78 00:05:27,670 --> 00:05:31,570 do you wanna play a game of Blackjack? Type yes or no. 79 00:05:32,800 --> 00:05:37,800 So remember that this entire function is going to be evaluated and the result or 80 00:05:38,620 --> 00:05:41,920 the output is going to replace this part of the code. 81 00:05:42,520 --> 00:05:46,000 So we can say that while the input is equal to y 82 00:05:46,660 --> 00:05:49,000 then we're going to keep playing the game. 83 00:05:49,630 --> 00:05:54,630 The game is all the code between hint 5 and the end where we compare. 84 00:05:57,140 --> 00:06:00,830 So if we actually put this inside a function by 85 00:06:00,830 --> 00:06:05,830 indenting it over and then creating a function here called play_game, 86 00:06:07,430 --> 00:06:10,280 well then we can actually call this function, 87 00:06:10,340 --> 00:06:13,940 play_game, every time the user types 'y'. 88 00:06:16,790 --> 00:06:20,810 Now we don't want to clutter up our screen with all the previous games. 89 00:06:21,050 --> 00:06:23,480 So before we call play_game, 90 00:06:23,540 --> 00:06:27,440 we also want to clear the console. And to do that 91 00:06:27,470 --> 00:06:32,470 we obviously have to import the clear function from the replit module. 92 00:06:33,110 --> 00:06:34,340 So from replit 93 00:06:34,340 --> 00:06:39,340 import clear, and now we can call this clear function to clear the console in 94 00:06:42,320 --> 00:06:43,880 here after each game. 95 00:06:44,450 --> 00:06:49,450 And then we can call play_game so that the code in this function gets repeated. 96 00:06:52,190 --> 00:06:55,220 And we also said that at the beginning when we play game 97 00:06:55,340 --> 00:06:59,330 we're going to show the user the logo from art.py. 98 00:06:59,930 --> 00:07:02,870 So let's go back and import that as well. 99 00:07:02,870 --> 00:07:05,960 So from art import logo, 100 00:07:07,580 --> 00:07:12,140 and then at the beginning of every game, we're going to print our logo. 101 00:07:13,190 --> 00:07:16,490 That's all there is to this project 102 00:07:16,880 --> 00:07:21,440 and now let's run it one final time and see the whole thing in action. 103 00:07:22,520 --> 00:07:25,040 Do you wanna play a game of Blackjack? Yes. 104 00:07:26,060 --> 00:07:28,730 And it reveals our beautiful Blackjack logo. 105 00:07:29,150 --> 00:07:32,390 It tells us the cards that we have, so current score of 11. 106 00:07:32,420 --> 00:07:36,590 I'm going to take another card. I'm on 21, which is perfect. 107 00:07:36,620 --> 00:07:41,150 So I'm going to pass now. And it tells me that my final hand was 21, 108 00:07:41,390 --> 00:07:46,390 the computer's final hand was 17 and I win. And now I can play another game just 109 00:07:47,330 --> 00:07:48,950 by typing 'y' again. 110 00:07:49,880 --> 00:07:54,200 And I can keep going on and on playing my game of Blackjack. 111 00:07:54,950 --> 00:07:57,620 So I hope you had fun at building this game with me. 112 00:07:57,980 --> 00:08:02,390 I know this was a really difficult project and I know that some of you would 113 00:08:02,390 --> 00:08:03,710 have probably had some issues, 114 00:08:03,710 --> 00:08:07,850 but I really hope that you persevered and you got through it in the end. And 115 00:08:07,850 --> 00:08:09,020 through the process 116 00:08:09,050 --> 00:08:12,740 you've managed to apply a lot of the things that we've learned in the last 10 117 00:08:12,740 --> 00:08:13,573 days. 118 00:08:13,760 --> 00:08:17,900 This is a good time to review the concepts you've learned and have a think about 119 00:08:17,900 --> 00:08:22,460 what you might need to revise or revisit so that you're really, 120 00:08:22,460 --> 00:08:24,740 really strong on all the concepts 121 00:08:25,070 --> 00:08:29,450 and you're comfortable to continue on to the next 10 days. Now, 122 00:08:29,480 --> 00:08:32,809 if you want to take a look at the final code that I've got here, 123 00:08:33,080 --> 00:08:37,400 then just head over to repl.it/@appbrewery/blackjack-final 124 00:08:37,820 --> 00:08:41,419 and you'll see all the code that I went through with you just now.