💫 Coding the Winning and Losing Scenarios | myBetabox

💫 Coding the Winning and Losing Scenarios

Keyboard Guide

Coding the Winning and Losing Scenarios

Stuck? Copy & Paste the Code into Repl.it

import random

player_wins = 0
comp_wins = 0

print("Welcome to RPS")
player_choice = input("Do you pick [rock], [paper], or [scissors]?: ")

comp_choice = random.choice(["rock", "paper", "scissors"])

print("Player chose " + player_choice + " and Computer chose " + comp_choice)

if player_choice == "rock" and comp_choice == "scissors":
  print("You Won! Congratulations!")
  player_wins += 1
elif player_choice == "paper" and comp_choice == "rock":
  print("You Won! Congratulations!")
  player_wins += 1
elif player_choice == "scissors" and comp_choice == "paper":
  print("You Won! Congratulations!")
  player_wins += 1
elif player_choice == "rock" and comp_choice == "paper":
  print("You Lost! Try Again!")
  comp_wins += 1
elif player_choice == "paper" and comp_choice == "scissors":
  print("You Lost! Try Again!")
  comp_wins += 1
elif player_choice == "scissors" and comp_choice == "rock":
  print("You Lost! Try Again!")
  comp_wins += 1
elif player_choice == comp_choice:
  print("You Tied! Try Again!")

The Code Explained

Now that both players have entered their answer, we can compare to see who won! This is the most complicated part because it uses conditionals to check to see who won. In Python, we use == to compare two things in conditionals.

🌟 Conditionals are ‘if’ statements that compare two values. If statements always have a : at the end.

Take a look at the below code for an example of how our comparisons will work:

IF I use rock against scissors THEN I win.

if player_choice == "rock" and comp_choice == "scissors":
  print("You Won! Congratulations!")
  player_wins += 1

This if statement checks to see if your answer was rock and the computer’s answer was scissors. If that statement is true then it will print out the congratulations message!

We also added 1 point to the score by incrementing the player_wins variable under that condition.

🌟 Incrementing is adding to a number. You can add 1 to a number by using += 1.

You may have noticed that some of the lines are indented, or starting after a couple spaces to the right. This is because of Python syntax for conditionals. If your final code isn’t working, make sure the lines that need to be indented are! You can so this by pressing tab on your keyboard.

Now, what about the other results? Let’s start by adding the rest of the winning conditions using an elif as you see below:

🌟 Elif stands for else if and is used to make outcomes for more scenarios.

elif player_choice == "paper" and comp_choice == "rock":
  print("You Won! Congratulations!")
  player_wins += 1

This conditional uses elif which stands for else if. This once again checks for a win condition and prints out the congratulations message if it is true. Make sure to notice that the print statement and player_wins are indented from the if statement. Python requires you to do this so do not forget it! Now you can write your last win conditional!

After writing all the win conditions, we need to write the loss conditions. They will look very similar but will have different messages. Take a look at this example:

elif player_choice == "rock" and comp_choice == "paper":
  print("You Lost! Try Again!")
  comp_wins += 1

Now that you have an idea of how to write the loss condition, go ahead and write the other two losing conditions as well before we move on!

We also need to look out for two other conditions. One of them is a tie. We can check for a tie by doing the following:

elif player_choice == comp_choice:
  print("You Tied! Try Again!")

The other option is that the player did not put in a valid answer because it wasn’t typed correctly or they used a completely different word. If this happens we need to tell them that they did not give a valid answer. You can do something like the following:

else:
  print("That's not an option and you know it!")

With that final line of code, we have a game that will work! Feel free to give it a shot and report back!