Effective PyCharm Transcripts
Lecture: Introducing variables
Login or purchase this course to watch this video and the rest of the course contents.
0:00 Highlight the value of this ability to create variables and other related items. I want to focus in on an incredibly simple little function here.
0:09 Get defensive role. So when a creature is attacked either it's you as the wizard or something that is being attacked like a dragon or a small animal.
0:20 There's going to be this call to get defensive role. If we look what's happening here it says well there's a three times a random number
0:27 between one and 12 times your level. So your defensive ability has something to do with your level. And what is this? Random?
0:35 Well remember this is like tension in the dragon. So that's a 12 sided die right there.
0:39 So you're rolling your dice, you're multiplying that by your level and then there's some
0:42 kind of three here. I think that might be like a modifier or something. Yeah so this is not entirely obvious what's happening.
0:51 We'll see if we can actually do a lot to make it much much cleaner. Another thing that I like to do a lot is I would like to put a
0:58 breakpoint here and see what the value of that role was. There's no real way to do it.
1:04 You can do division like a divide by the level and by the three and then get the number back and figure out what it was.
1:10 But that's not the same as just seeing the value in the Debugger. Let's create a variable for this. It'll give us a good name.
1:17 So we don't need a comment and it will give us a variable that we can inspect in the Debugger and it will shorten this thing up.
1:24 So what we can do is we can ctrl+t to re factor again and we can introduce a variable. Now it takes a guess as the return value of randint
1:33 And so is it random? No this will be dice or die roll like that. What do we get when we rolled our 12 sided dice?
1:42 We got this or value. This is so now it's getting to be clear what's happening. The defensive role is some number we're not sure about yet.
1:51 Times the dice the dice roll times your level. That's pretty straightforward. Now let's say this is a modifier and maybe three is usually
2:00 what we use but in certain circumstances we might want to even change it. So we'll create a variable for this as well and I is not ideal.
2:07 So let's call it modifier and maybe even put it like this. So now we have a modifier times the die roll times the level that is so
2:17 much clearer. Now remember this is a really small example, it was literally that one line of code but it's already getting more clear and if
2:25 you apply these ideas at say the function level you grab three or four lines of
2:29 code and you give it up function that has a name even better but naming these variables here already is making a big difference.
2:38 Now I did say we want the modifier to be adjustable by default. I wanted to be three but I would like whoever calls this function to potentially some
2:46 other circumstance, passing a different modifier. So what we have here. Well obviously that's not gonna work.
2:52 So let's go and actually put this back so that will show us the anti create variable. We go over here,
2:57 stay in line, PyCharm says we're going to put this into one location. There we go and now we're back. We have three times that I stroll times level.
3:06 Now to make this configurable, let's make it a parameter to this function. So let's go here and we can say we want to introduce a parameter,
3:15 not a perimeter object. That would be interesting. But there's just one thing. So this will do.
3:19 And up here it says I notice it gave it a default value. That means whoever whatever part of code is calling,
3:27 get defensive role without passing a modifier because there's a default value, it will continue to work as is now if we want to pass a value,
3:35 we can The name i is not ideal. So let's give it a modifier and then we have it. It's very clear what the mechanism,
3:42 what the function for defensive role is. It's the modifier, times the dice roll,
3:46 times your level. Boom. But we did that by introducing variables where we had
3:51 just little computations and expressions, let's run this out by just seeing what happens if
3:55 I hit the do bugger this time around and I go and attack whatever. Check this out. The dice roll was four.
4:05 The modifier is three and my level Level right there, 75 75. Awesome. So it also has enhanced the debugging experience because now I can
4:14 see exactly all the pieces in play. We'll get to debugging more later, but it's definitely a benefit of this little change we made.