Python for Absolute Beginners Transcripts
Chapter: Using external Python packages
Lecture: Demo: Adding color output with colorama

Login or purchase this course to watch this video and the rest of the course contents.
0:00 It's time to start using our Colorama. Lets go and run this really quick. Here you can see we've got rock, paper, scissors version here's the header.
0:08 Maybe we want the header to be like a cool pink or something like that. And yet here it's just gray. Maybe we also want to ensure
0:16 that the background or the default color is white. For example, if I come over to my prompts my terminal here
0:23 you can see that my normal colors are yellow. And if I were to type Python3 everything's yellow. So for example, if I run this program
0:33 I copy this from there. I'm going to make sure I use the Python from that virtual environment to run this file. Everything is now yellow.
0:40 It'd be nice to have a baseline color for what we're doing here. So what we can do in the beginning is we can just start, right at the top of the file.
0:47 We can just print out some kind of text or something using Colorama to make it that color. So the way we do it is say colorama.
0:54 or dot, and then whatever color we want. Let's go with white. Now if I do this over here and run it see everything's yellow.
1:03 But as soon as I run it it's turned everything white. And as we work with it, it's just going to stay white. For now, okay?
1:09 The program exits then it resets. So this kind of does a baseline color for us and what else do we want to do?
1:16 Let's go over here, I'm going to copy this. I'm going to use it a lot. I said we want to have the header have a color, right?
1:21 Let's go over here and say print or dot. What do we want here? Magenta, maybe? Magenta's going to be good enough. Now we can do it like this.
1:31 We can come down here and say that plus text but this is so big I'm just going to put it like this. And we want all the stuff to be here.
1:39 And then we got to put it back, colorama.Fore.WHITE. Okay, well one thing that we're doing a lot of is saying colorama.Fore.that.
1:48 Now here, normally I like this format. I like this format because it's really clear where all this comes from.
1:54 But we're going to be combine it with a lot of text so we can do it, import a different way. Let's roll this back a little bit.
2:00 Notice there's an error. We come over here and say, From colorama import, the stuff that's in there which is all those things.
2:09 And just say for, that arrow goes away. If you don't type this by the way PyCharm knows that you should have typed it
2:15 so we go over here and we can hit Alt + Enter and it says, Do you want to import this? You want colorama.Fore? Yes I do, and it'll just write that line
2:22 as if we had done it up there. All right, so that's a lot of talking. Let's see what we're getting here. this up and try it again. Check it out.
2:33 We have our color version up here of our external libraries not really sure that I'm digging this V2 thing so let's do it like that.
2:41 Cool, Rock, Paper, Scissors, this edition then if I type this we're not quite seeing anything yet are we? So let's go back up to the top.
2:51 Our player's logged in. The log, it goes to a file. It doesn't make any sense, I tried to make it color it won't respect that anyway.
2:58 So the next thing to do is let's go over here, let's go down here let's, for all the errors, give them a color, huh?
3:04 Come down here and say Fore.LIGHTRED or RED. There we go. So let's go and see if we can get a color here that's not so good
3:12 or a output that's not so good. So my name is Michael, I want 99, try again. Notice we didn't reset it. So let's see, we could do that.
3:24 Fore.WHITE again. There's some cool coding techniques and stuff that we could use little data structures that we could come up with
3:31 to have that automatically reset but for now let's do it like this. It's going to be Michael and 99, try again. See how easy this is?
3:39 For us to see what's happening? Okay, let's see. For the round we're going to roll these things. player_1 rolls this.
3:46 Let's make it really clear who's rolled what so for you the player what are we going to use? Let's go with some yellow.
3:55 For your opponent let's have a light blue and then let's reset it like that. Keep going. And by the way, I'm running
4:03 because it's more clear, the color stands out more but you can run it in PyCharm and see those colors. You see them down here for example. Do 99.
4:12 It comes up but it's not quite as clear because the gray background. So let's go and keep running up here. Michael, 99, no, let's play again.
4:21 I'm going to throw some sponge, love some sponge. Michael rolls, roll sponge? Computer roll scissors. This should really stand out here.
4:30 Who takes the round maybe even the scores here we could do some work. Fix the S, and here we can put we don't want to log that for thing
4:42 so we want to put that here on this output. We could do this we could even say something like this we could say four equals the four there.
4:50 I could say this if winner equal to player_1 else that means you've lost so Fore.RED, light red, something like that.
5:01 Let's try that here, I think that'll be fun. So come down here, I'm going to be Michael. I'm going to throw some scissors
5:07 oh, we didn't put it back, did we? Look, I won the round! We could do it like this. White and the same message there, that should do it.
5:17 Okay, try again. Michael, throw some sponge. Computer takes the round I'm going to throw some air, some more air aw man, that computer is tough.
5:26 Let's try two, three, three, it was tied. Computer takes the round. I'm telling you, this computer has got my number I cannot beat this thing.
5:35 Seven, two, yes! I've taken a round! Remember it was kind of hard to scan this you had to read over it, What's going on? and so on.
5:45 We're also going to improve this so it doesn't take up so much space later but for now we're getting our color there
5:50 and then, yeah, we're pretty much done. Let's do one more thing here for this color here. Let's put, yeah we don't need to do it.
5:58 We could print out some stuff but we'll keep it there. Again we'll say Fore equals let's get the same little test here. If overall winner is that
6:12 and then we just want to do Fore plus. And let's reset it, Fore.WHITE. Actually, careful, we don't want to log that.
6:20 Not sure what'll happen if we put that in the text file but it's probably isn't good. So here, let's play one more round take it all the way through.
6:26 Michael, I'm going to play some sponge because that's my favorite one. They played scissors which cuts the sponge aw man, I cannot win.
6:34 All right, I took a round with some rock do some more rock. Getting close, yes! I take the round, and I win the game, incredible!
6:41 But down here we have that coming out of the bottom. Hopefully you get the sense that just this little bit of extra color
6:48 really makes this game easier to play and I think a little more entertaining as well and it was all made possible by Colorama.
6:55 So Colorama is this public thing out on the internet we're able to download it and install it with pip into our virtual environment here.
7:02 Even expand it and probably see somewhere the packages are installed. Yeah, there's Colorama toolkit and others.
7:09 But you don't mess with that, you just leave that alone alright? Same thing over here. packages, we don't want to mess with these
7:14 but you can see that they're there. The best way though is to go over here and say pip list and it'll show you what's installed what version and so on.
7:23 Alright! Very cool, we made our app better by using one of these external libraries. Well that's a start. We used one.
7:30 There's 215,000 plus more to go explore see what cool stuff we can do.


Talk Python's Mastodon Michael Kennedy's Mastodon