 Right, so hey guys and welcome back to another Python tutorial So as you may know in the last tutorial I showed you guys how to use dictionaries for beginners and how they can pretty much be used to carry out a basic task So I didn't go much in depth about how they can actually be used to implement as a solution where it can prevent a lot of logic So in this tutorial, we're going to be using dictionaries to create a rock-paper-scissors game But we're not going to have any GUI. It's just going to be a console-based game So without further ado, let's begin So today I'm going to try and improve the quality of my videos by starting to use visual studio code Which was a recommendation by someone that has either subscribed to me or is on discord on my channel So here it is Let's start by opening visual studio code Which I started using recently and it's really a lot better than the default idle that I use So first of all, I'm going to go ahead and create a new file and then save that as I said that on my desktop and call it rps.py rps stands for rock-paper-scissors if you didn't know So first off what we're going to need for this is To actually be able for the computer to make a guess between rock paper and scissors For this we're going to be using numbers instead. So in python, we have something called a random module Which helps which helps us produce random numbers So this is what we're going to be doing So we're going to be importing random because we need Random guesses about either rock paper or scissor from the computer So we're going to be using random numbers and converting them into a user into the computer's choice of whether the The computer selects rock paper or scissor So once we're done with import random, let's hop on to creating the actual main loop of our game So main loop And in here, I'm going to start by saying while one which means while true It's going to start a continuous loop. So never ending loop Now we need to create a new variable which will store our random number. So random number equals random dot ran int Um, and then we need to give it a number range. So I want a random number between one and three So once i'm done with that, I have a variable now that's called random number. That's assigned to a Random um integer between one and three So each time I run this program the program should return a number randomly between one and three I'm going to quickly go ahead and zoom into the screen a bit so that it's easy for you guys to see Cool, sorry about that. I just realized it was quite small Now the next line i'm going to actually print out this random number just to show you how it works So now if I go ahead and open cmd And then go on to cd desktop Python rps.py If you see right here, there's a there's a whole lot of numbers being spammed But if I highlight them, there's different numbers being spammed each time Now it's being spammed really quickly because it's in a Unstoppable while loop and so it will keep giving me random numbers until I actually stop it So that's a random number solved Now this random number will later have to be converted into a choice of rock paper or scissor Which is how we're going to determine what the computer has chosen Now let's leave that for a bit later What we're going to do next is we're going to gather the user input So i'm going to leave a line here because we need to do something else there. So user choice equals input String and then we need to ask the user what they want to actually select so select rock Or paper or Scissors so this needs to be accurate so the user is going to have to enter either rock paper or scissor And they need to spell it correct correctly, which is why we're going to be adding error handling for it as well Just in case they spell it wrong cool So now we've got our user input and we've got our random number Now this random number right here. We're going to need to change that into the computer's choice So we're going to do that by actually creating a new function that's going to be converting the number into a choice First of all, I'm going to go up here and then create a new function call it def converted Outcome and then in this function, we're going to pass in the number that is produced over here So whatever random number is produced over here will be passed into this converted outcome Function into this numbers parameter right here So what I do next is I'm going to say f number. So whatever number is being passed, which is a random number equals one then return Rock so I'm saying that if the number that's randomly produced is one Um, the number one would be assigned to rock We do an ellip number equals two I'm going to return paper. So if the number that's randomly produced is two then we're going to return paper and then lastly If I do a left number equals three return scissors So now we've got all the three numbers into actual valid rock paper or scissors outcomes Now we're going to have to run this function right here on line 15. So I'm going to do computer choice. So that's my new variable computer choice equals converted outcome and in the brackets I'm going to pass in the random number because that number is going to change each time So the computer choice will changed to two. So now down here after the user and computer choice once we're done with that I'm going to quickly print computer choice And then I'm going to print User choice, let me just scroll down a bit here Uh, I'm going to print user choice. So user choice and now if I quickly open up my command prompt Okay, I guess I closed it by accident. I have to do that again option it to leave CD desktop and python rps.py Cool. So it's asking me to select rock paper or scissors. I'm going to go with rock And now as you see up here The computer's choice is shown first. So the computer selected paper in this instance and since we typed in rock We have rock. Let's do it again. Computer should either change should pretty much change The um choice this time. Sometimes we get stuck on the same one But hey as you see right here this time the computer chose scissors and we chose paper. Let's try again. Let's try rock Now it was a tie because the computer selected rock and we selected rock two. How about we try scissors Computer selected scissors too. How about we try that again? Okay Keep selecting. Okay, finally So the random number may sometimes get stuck on the same number in this instance For example, we typed in scissors three times or actually four times here And out of four times only one time the computer actually changed to paper So that's completely normal in this scenario where the computer and we choose the same Um avatar. Let's call it for now. It's going to be a draw So now I'm going to quit this loop by pressing ctrl c or ctrl z All right, cool. So that's the error that we got which is fine. Now I'm back on my Command prompt again minimize that now we've got as far as having the two outcomes ready Now what we're going to do next is do our error handling here So before that we need to actually set the logic for this game. So this is the interesting bit We're not actually going to be using if statements. So instead of doing the typical tutorials that show you to do if computer choice I don't know equals Rock And Or is it I'm not sure what and is in python, but I'm assuming it's and user choice equals paper print Computer wins Actually, I mean user wins This is jarring because we're going to have to write a load of if and help statements But instead we're going to be using the clever way and using a dictionary which we learned how to use in the last tutorial So I'm going to have a Dictionary up here. I'm going to call it game outcomes So this dictionary is going to store pretty much all the outcomes that can actually happen in this game and the results to those outcomes May sound confusing for now, but as we code you want to send We'll call this outcomes Equals and then we're going to open a dictionary Now this is going to be a nested dictionary. So inside this nest inside the first outcomes dictionary The first key value is going to be rock Now assigned to rock is going to be the different outcomes that could actually occur with rock And um, so what could be played against rock and what the outcome of that would be So the key of the I mean the value of the key rock is going to be the different outcomes So if rock is played against rock The outcome would be draw Then we use a comma If rock was played against um paper Then we'd have a loss Because we lose and if rock was played against scissors Then you'd have a win Oh, so that's all the um events that could happen in the place of a rock So that's all the outcomes and an event that can happen when rock has been selected by either computer or the user Let's move on to paper Same way we have another key value pair where we're going to be assigning the value to another dictionary So paper against rock Is obviously a win. So paper against rock is going to be a win comma Paper against paper is going to be a draw comma paper against scissors is going to be a loss Because obviously scissors would win now last but not the least we are going to do scissors So what are the different events that could occur in the in the case of scissors being um selected? So if scissors are selected and they use against rock We would have loss comma if the scissors are selected against paper We would have win And lastly if the scissors are selected against scissors make sure you're spelling it correctly Then we would have a draw Cool. So that's literally all the possible outcomes that we could have in this game And as you see we we are not choosing a lot of ifs if and else statements here Which makes it a lot cleaner than if we had done it that approach So now we've got this ready. This dictionary is all ready to go So what i'm going to do next is actually show you guys how to implement the logic So we have now the computer's choice and we have the user's choice and we've got to find out who wins So in our loop, I'm going to go next and actually type in a try and accept statement because Just in case the user enters anything apart from rock paper scissors. We need to handle that error. So try print I'm going to print use selected um And then the user input. So whatever the user has selected user choice Then I'm going to print The computer selected and then whatever the Oops, I forgot to come out there come up And then whatever the computer has selected computer choice, and then I'm going to do print um asterisk game outcome, which is where the actual outcome of the game will be shown And then last but not the least this is the main bit where the logic gets in place. So we're going to print Outcomes Which is the name of our dictionary Then we're going to print user input, which is the user's choice And then we're going to also oops Okay, come on And then we're also going to use that against the computer choice So this might not make sense right now, but let me explain So I'm going to do that and I'm going to print a blank line as well Let me just uh complete the accept statement So exception statement and then I'll explain what we've just done there print So if anything apart from rock papers is entered or any error happens we type in invalid inputs Cool. So now what we've done over here is I'm going to quickly zoom out of my screen so I can have the Dictionary in the same view as well Cool. So all we've done over here is we've said Actually, I'm just going to control c Do not do this. I'm just doing it for explanation purposes Okay, there we go. We're back to square one. So we have print outcomes user choice and then computer choice So when we use square brackets, we're pretty much accessing a value of a key So using a key. I mean so when we go outcomes We go in this dictionary right here Next we are using the user choice now. Let's say the user has typed in rock If the user types in rock, we will be right here So that's going to be the key we're on and the value is going to be this entire dictionary If the user selects paper The key is going to be paper and then this is going to be the value the entire Dictionary of available events for paper and then if the user selects scissors The key is going to be scissors and then the value is going to be this entire dictionary for the values of scissors, cool Now let's assume the user has selected rock and then the computer choice Somehow comes down to being paper So rock is going to be the user's choice. So we're right here And then based on that against that we're accessing The paper paper key inside the rock Value dictionary. So what is the value for the paper key loss? So rock against paper comes down to a loss Now let's assume the user's choice was paper So if the user choice has been paper and the computer's choice was I don't know Let's say scissors then our value is going to come down to loss So no matter what the user and computer enters This is a full proof program that will run based on any event and give you the right outcome Cool. So now that we've finished with all of it enough of the blabbering and let's actually run this to see if it works So i'm going to go to my command prompt once again and Run this. Okay. User choice is not defined User choice. Okay. I see. I forgot to take out this dummy line I had right here. Sorry Oh, let's go back Uh, select rock paper or scissors. Let's just give it a fresh one Uh, I'm gonna screen rock paper scissors rock You selected rock computer selected rock game outcome was a draw. Let's go rock again I selected rock computer selected paper And I got a loss because paper against rock is obviously a loss for me. Now. Let's go with rock again I selected rock computer selected rock again, and it was a draw. Let's go with paper I selected paper computer selected paper. It was a draw paper again I selected paper and the computer this time on the contrary selected rock So that was a win on me and lastly we're gonna try scissors And then the computer selected paper So we got a win and now let's try and check our error handling. I'm going to type in some gibberish It says game outcome invalid input. So whatever happens, it is not going to crash You guys can also implement scores if you like by Simply replacing these values instead of draw loss or win You can enter something like a number in here and like I don't know Like if it's a draw you can enter a score of one and if it's a loss you can enter a score of zero And for a win you can enter a score of two totally up to you And then you can extract that number and then update the user score as you go So if you guys would like a tutorial on how to do that Please request that in the comment section if you want to have like a score section in this game as well And then the game ends when a specific user reaches a certain amount of score And then it announces the score that user to be the winner So, hope you guys have enjoyed this tutorial. It's been Finally a short tutorial after a while of posting long tutorials So I hope you enjoy this light tutorial Hope you like the use of visual studio code now because it's not going to be too intensive on your eyes I just realized how much that helps and it also helps me code a lot more better because of suggestions and all that Like always guys, if you have any suggestions for other videos Do make sure to drop them in the communities have under my post If you guys would like to support the channel directly you can purchase one of the Um emojis custom emojis in the super chat or you can purchase a highlighted message I'm not pressuring anyone to do so. But if you'd like to do that out of gratitude go ahead um What was it? I'm going to also release another tutorial based on how to produce a gooey version of this game using tiginta So hope you're excited for that It might not be the next video after this because it may take me a bit while To try and figure out how I'm going to do that. But as soon as I'm done with that, I'm going to post it So guys, that's it for today's tutorial and I will see your beautiful faces in the next tutorial. Peace out