1 00:00:00,480 --> 00:00:02,494 Instructor: Welcome back. 2 00:00:02,494 --> 00:00:03,660 Let's do a fun little exercise. 3 00:00:03,660 --> 00:00:07,260 We just got hired by a gaming company 4 00:00:07,260 --> 00:00:09,570 and we're creating this wizard game. 5 00:00:09,570 --> 00:00:11,820 And this wizard game has the, 6 00:00:11,820 --> 00:00:16,140 is magician in the user's profile. 7 00:00:16,140 --> 00:00:18,480 And for now, we'll just set it to false. 8 00:00:18,480 --> 00:00:21,880 We also have is expert 9 00:00:24,930 --> 00:00:28,032 and whether this user is an expert at this game, 10 00:00:28,032 --> 00:00:30,519 we'll leave it at true for now. 11 00:00:30,519 --> 00:00:34,293 And then, hmm, let's do this here. 12 00:00:35,321 --> 00:00:40,098 I want you to check if both 13 00:00:40,098 --> 00:00:45,098 or check if magician and expert 14 00:00:47,280 --> 00:00:50,670 and if that's the case, if true, 15 00:00:50,670 --> 00:00:53,190 then I want you to print 16 00:00:53,190 --> 00:00:57,240 you are a master magician. 17 00:00:57,240 --> 00:01:02,240 And also I want you to check if magician but not expert. 18 00:01:07,770 --> 00:01:10,530 If that's the case, I want to just print 19 00:01:10,530 --> 00:01:13,653 at least you're getting there. 20 00:01:15,060 --> 00:01:19,920 And then finally, let's do a check that says 21 00:01:19,920 --> 00:01:24,851 if you're not a magician, I want you to say, 22 00:01:24,851 --> 00:01:29,657 well you need magic powers. 23 00:01:32,460 --> 00:01:35,160 Now I want you to use what you've learned 24 00:01:35,160 --> 00:01:40,160 about logical operators to create this conditional logic. 25 00:01:40,290 --> 00:01:41,820 You can pause the video if you want 26 00:01:41,820 --> 00:01:44,580 and try it yourself in a repel. 27 00:01:44,580 --> 00:01:47,670 Otherwise, I'm gonna keep going with an answer. 28 00:01:47,670 --> 00:01:50,070 But you should be able to solve this fairly easily 29 00:01:50,070 --> 00:01:53,703 if you've been following the videos and exercises so far. 30 00:01:55,770 --> 00:01:57,750 So let's start with the first one, 31 00:01:57,750 --> 00:02:00,840 where we say you are a master magician. 32 00:02:00,840 --> 00:02:05,840 So I'm going to say if is expert and is magician, 33 00:02:11,888 --> 00:02:16,470 I am going to print, you are a master magician. 34 00:02:16,470 --> 00:02:18,183 So I'm going to copy that and say, 35 00:02:19,324 --> 00:02:22,500 print you are a master magician. 36 00:02:22,500 --> 00:02:23,793 Nothing too crazy here. 37 00:02:24,660 --> 00:02:29,523 And then next one I'm going to say if, 38 00:02:30,725 --> 00:02:33,870 or I could even say elif if I want, and again, 39 00:02:33,870 --> 00:02:37,890 this is something that you can decide what you prefer. 40 00:02:37,890 --> 00:02:41,640 I usually like using elif if it's part 41 00:02:41,640 --> 00:02:44,610 of the same concept as the first one. 42 00:02:44,610 --> 00:02:46,920 In this case, yeah, we're, we're still trying 43 00:02:46,920 --> 00:02:51,360 to figure out what type of user it is so we'll use elif 44 00:02:51,360 --> 00:02:55,800 and we'll say elif, we wanna check if it's a magician, 45 00:02:55,800 --> 00:02:59,910 so that is is magician. 46 00:02:59,910 --> 00:03:02,820 So we're going to check if that's true, 47 00:03:02,820 --> 00:03:07,820 but we also wanna check if, well not an expert. 48 00:03:09,180 --> 00:03:11,310 Now how do we do this? 49 00:03:11,310 --> 00:03:16,310 I mean we could say use short circuiting and say is expert. 50 00:03:18,180 --> 00:03:22,426 And even if this is false, we can get true here 51 00:03:22,426 --> 00:03:27,426 because is a magician, short circuit and print this part. 52 00:03:28,470 --> 00:03:29,970 At least you're getting there. 53 00:03:33,930 --> 00:03:35,860 Because the interesting here is 54 00:03:37,829 --> 00:03:39,510 that this code block is never going to run 55 00:03:40,479 --> 00:03:41,820 if both of these are true. 56 00:03:41,820 --> 00:03:44,520 Because in the first if block, we're checking that, 57 00:03:44,520 --> 00:03:47,040 so that as soon as this is true 58 00:03:47,040 --> 00:03:50,160 we're going to completely ignore the elif. 59 00:03:50,160 --> 00:03:52,548 So we could do that, but, hmm, 60 00:03:52,548 --> 00:03:55,980 this is a little bit hard to read 61 00:03:55,980 --> 00:03:58,620 because I really have to think about it. 62 00:03:58,620 --> 00:04:00,579 If I come back to the code 63 00:04:00,579 --> 00:04:01,883 or somebody else looks in my code, 64 00:04:03,560 --> 00:04:05,197 it's not clear that unless I comment 65 00:04:05,197 --> 00:04:07,820 that I'm checking if it's magician but not expert. 66 00:04:07,820 --> 00:04:11,660 Because this will never run if is magician is false, 67 00:04:11,660 --> 00:04:15,024 then that's what they're checking for. 68 00:04:15,024 --> 00:04:19,059 I know it's kind of confusing even saying it. 69 00:04:19,059 --> 00:04:22,530 So I prefer using something like this, 70 00:04:22,530 --> 00:04:25,353 using and not, 71 00:04:27,900 --> 00:04:30,564 and that reads a lot better, right? 72 00:04:30,564 --> 00:04:32,850 It makes a lot more sense. 73 00:04:32,850 --> 00:04:36,390 Hey, if this person is an expert and magician, do this. 74 00:04:36,390 --> 00:04:39,660 Otherwise, if this person is magician 75 00:04:39,660 --> 00:04:43,322 and not an expert, then print this. 76 00:04:43,322 --> 00:04:45,060 That reads pretty nicely. 77 00:04:45,060 --> 00:04:47,583 And yes, you can do this because remember, 78 00:04:48,541 --> 00:04:49,374 not as simply checking 79 00:04:49,374 --> 00:04:52,020 and inverting whatever the bullion expression is. 80 00:04:52,020 --> 00:04:57,020 So this still evaluates to a bullion that and can check. 81 00:04:57,023 --> 00:04:59,970 All right, let's finish the final one. 82 00:04:59,970 --> 00:05:03,030 If you're not a magician, you need magic powers. 83 00:05:03,030 --> 00:05:05,010 So how do we check here? 84 00:05:05,010 --> 00:05:08,284 Well, we can do another elif and say, 85 00:05:08,284 --> 00:05:12,543 not is magician. 86 00:05:14,850 --> 00:05:16,650 And if I run it like this 87 00:05:16,650 --> 00:05:19,560 and say print and we'll just copy here, 88 00:05:19,560 --> 00:05:21,333 you need magic powers. 89 00:05:24,360 --> 00:05:26,010 And let's remove the comments for now 90 00:05:26,010 --> 00:05:28,090 just so we can see the blocks of code 91 00:05:29,220 --> 00:05:31,464 and we're gonna test our program. 92 00:05:31,464 --> 00:05:32,970 Hopefully we coded it properly. 93 00:05:32,970 --> 00:05:36,510 Hopefully there's no bugs, but let's have a look. 94 00:05:36,510 --> 00:05:41,510 If I run this, you need magic powers. 95 00:05:41,580 --> 00:05:42,420 Why is that? 96 00:05:42,420 --> 00:05:47,400 Well, if magician is false, none of these are going to work. 97 00:05:47,400 --> 00:05:49,946 What if I change this to true and I click run? 98 00:05:49,946 --> 00:05:54,030 All right, I am a master magician 99 00:05:54,030 --> 00:05:57,840 because I'm an expert as well as a magician, 100 00:05:57,840 --> 00:06:00,843 What if I turn this to false and I click run? 101 00:06:02,040 --> 00:06:03,933 Hey, at least you're getting there. 102 00:06:05,307 --> 00:06:07,200 I'm a magician, but I'm not an expert yet. 103 00:06:07,200 --> 00:06:10,053 I'm still, I still need to improve my gameplay. 104 00:06:10,950 --> 00:06:14,700 Now you may have gotten a different answer than me 105 00:06:14,700 --> 00:06:16,800 and there's many ways to do this, right? 106 00:06:16,800 --> 00:06:19,050 You could have used an elif statement, 107 00:06:19,050 --> 00:06:21,030 you could have used or. 108 00:06:21,030 --> 00:06:24,240 Conditional logic is all about writing code 109 00:06:24,240 --> 00:06:26,700 that makes sense not only to you 110 00:06:26,700 --> 00:06:29,160 but for other people reading your code. 111 00:06:29,160 --> 00:06:33,180 The idea is not to be extremely clever 112 00:06:33,180 --> 00:06:35,370 or really worry about short circuiting. 113 00:06:35,370 --> 00:06:38,280 Yes, it can be more performant, but a machine is so powerful 114 00:06:38,280 --> 00:06:41,681 that just tiny bit of optimization there is usually 115 00:06:41,681 --> 00:06:46,264 not worth it when you're losing perhaps readability. 116 00:06:46,264 --> 00:06:50,790 So you wanna make sure that your code reads like English. 117 00:06:50,790 --> 00:06:53,440 And this is a topic that, frankly, 118 00:06:53,440 --> 00:06:57,990 a lot of beginner programmers have a hard time understanding 119 00:06:57,990 --> 00:07:00,750 where they tried to be as clever as possible, 120 00:07:00,750 --> 00:07:02,670 write as little code as possible 121 00:07:02,670 --> 00:07:04,500 in the shortest possible lines 122 00:07:04,500 --> 00:07:07,890 when instead you wanna focus on readability. 123 00:07:07,890 --> 00:07:10,747 Because a code that is easy to understand 124 00:07:10,747 --> 00:07:14,850 is a sign of a good developer. 125 00:07:14,850 --> 00:07:17,820 And I hope looking at this, you're thinking, yeah, 126 00:07:17,820 --> 00:07:19,530 this is pretty easy. 127 00:07:19,530 --> 00:07:23,940 I mean, this all makes sense, but this is not the only way. 128 00:07:23,940 --> 00:07:28,080 Maybe my version of what reads well is different than yours. 129 00:07:28,080 --> 00:07:29,760 The idea is to each of us 130 00:07:29,760 --> 00:07:33,485 try our best to make our code simple and nice. 131 00:07:33,485 --> 00:07:36,780 And in programming there is no one right answer. 132 00:07:36,780 --> 00:07:39,450 There are many ways to solve a problem 133 00:07:39,450 --> 00:07:40,890 but the key is to solve it 134 00:07:40,890 --> 00:07:43,713 in a simple manner that is readable. 135 00:07:44,970 --> 00:07:47,620 I hope you have fun and I'll see you in the next one.