Learn Python: Build a Rock-Paper-Scissors Game! #learnpythonin2024 #mlwithakansha

Akansha Yadav builds a rock-paper-scissors game in Python using the random library and nested if-elif-else statements. The tutorial covers importing random, capturing user input, generating the computer's move with random.choice(), and writing conditional logic to determine winners and ties.
Chapters
So hello everyone, welcome back to my YouTube channel. In today's video, we are going to create one mini project which is rock paper scissors game. So first, let's define the workflow of the project. So in the quotation marks, I'm going to define it. Workflow of project. First is, uh, we will be taking input from the user, which is gonna be rock, either it would be paper, or it would be scissor, because this is a rock-paper-scissor game. Second thing is, I will be taking computer choice, because this game is gonna be between user and computer. So computer choice I'm going to take, where computer will randomly choose something. Then the third thing is that I will be printing the results. This is gonna be whole workflow, that this is how our project is going to work. Next is there would be cases for either rock, either paper, or scissor. So let's suppose I want A for rock. I want the user to press A if you— if they wanna choose rock. If there would be rock and rock, like if the computer is also choosing rock and a user has also input rock, then in that case it would be tie. So it would be tie. Another would be— okay, this is just the keywords, so it's taking up from there. Another is if suppose user has selected rock and the computer has selected paper, so it should be paper wins. Let me keep it in the quotations. Okay, so if user is selecting rock and computer is selecting scissor, In that case, it should be rock win— should be rock wins. Another is the B1, which is gonna be rock-paper. So I want user to select B in case they wanted to choose paper. So after that, I will say paper— if it is paper-paper, then it would be tie. And if it is rock Sorry, paper and rock. So this time we have a paper, so I'm taking it common for every time. Paper and I would say rock, so it would be papers win. And if it is paper and scissors, then in that case scissors will win. Another is the C case, in which it would be Caesar. So let's suppose user has selected Caesar, but the computer has also selected Caesar. In that case, it would be again tie. If user has selected Caesar and computer has selected rock, then in that case it would be Rock wins. And in case suppose user has selected Caesar but a computer has selected Paper, then in that case it would be Caesar wins. So that is gonna be whole conditions which we are going to implement and the whole working process. Let me quickly check something. Okay. So now let's write the code. So for code, we are going to use one library which is random library. So import random, and from item_list, import item_list. Now I'm going to create the item list which is going to be rock, comma, paper, comma Caesar. In the workflow, as we have already defined that we are going to use A for this and B for this, so we will be defining the user choice. So suppose user_choice is our variable, and we are going to take input in this. Input, enter your move, which is either rock— either they can type directly the paper or scissors. In this case, we are giving the direct input. Let me correct the word input over here. Scissor. And then in the next one, I am going to say that it's going to be computer's choice. So comp choice is the variable which I'm creating. random.choice choice. And it means from the random library, I'm going to take the random, uh, reselection for the computer. Done. From the item list, it will take one random choice and that will be stored in the comChoice. So we have both the choices, user's choice and the computer's choice. So now we are going to print what are those choices. So I'm going to say print, I'm going to use this format method, user choice is— come out of it and I will write— okay, I'm using the method which is— so I don't need this. I will directly say is, and I will write it in curly brackets, user choice. And then I wanted to print computers choice is curly brackets compChoice, and I will close the quotations over here. And that's it. So we have taken both the choices, we have printed it. Now our next turn is to write all these conditions that we have defined above, that if in case the user's choice and the computer's choice is same, then in that case it should be tie. So we will write it using the if statements, if-else statements. Basically, we will be using nested if-else statements. So now let's write if user_choice is equal to equal to comp_choice, user U, I have taken capital. If both are same, then in that case, if, uh, choice, then in that case I just wanted to print both chooses the same, or we can say, yeah, okay, is equal to match type. This is our first condition which was at the top, that if both the conditions are same, then, then elif, let's suppose they have different choices, then in that case we will compare user's choice. Let's suppose user's choice is rock. I'm directly taking rock, I'm not taking ABC, I'm just taking it directly so that you— we will be giving it like, we will be modifying it further. So if user's choice is rock and if computer's choice is equal to equal to paper, then in that case what should happen? Then in that case I wanted to print directly that paper covers rock, and I will say that this time computer wins. Another time I'm going to use else statements, and then I will write print, okay, print rock smashes scissors is equal to you win. You means user. In this case, I'm directly using you, you just to make the user feel friendly with the game that we are creating. Next condition that we have is, let's suppose elif I'm taking it as per the user's choice. So I am taking user— let's scroll down— user's choice, let's suppose, is paper this time. In that case, what should happen if computer's choice is, let's say, it was paper, then this is scissors. So in case of scissors, what I wanted to print is I wanted to print, say, that Scissor cuts the paper and then computer wins. Scissor cuts the paper, yes, and then computer wins. Else, what I wanted to print is else print in the quotations that paper, uh, covers the rock, you win. Elif, user's choice, let's say, is Caesar. Then if computer choice this time is paper— we already have taken computer's choice paper, Caesar. Okay, we will alternate it. So what we will do is over here I will say print in the quotations, scissors cuts the paper, comma, you win. Else I'm going to make the computer win. Else print rock smashes scissors and computer wins. Let's run this program. Close this. Enter your move. So rock, user's choice is rock, computer's choice is rock. Have we done any mistake over here? Because none of the conditions are working for us. Okay, we are using print, print, print, and we are closing everything. Let's save it and rerun this. First, I wanted to make the change over here just to add one difference, and then I have some results. So let's check it. So yeah, now we have to enter the choice. Let's say I'm saying rock. So it says user's choice is rock, computer's choice is paper, and paper covers the rock, so computer wins. We can run it again to replay it. So I'm saying, let's say, paper. It says user's choice is paper and computer's choice is paper, so this time it's match type. So that's how simply you can create rock paper scissors.










