πŸ” Loop to Replay | myBetabox

πŸ” Loop to Replay

Keyboard Guide

Loop to Replay

Stuck? Copy & Paste the Code into Repl.it

import random

player_wins = 0
comp_wins = 0

playing = True

while playing:
  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!")

  print("Player Wins: " + str(player_wins) + " - Computer Wins: " + str(comp_wins))

  stop = ""

  while stop not in ["y", "n"]:
    stop = input("Would you like to stop playing? [y]/[n]?: ")

  if stop == "y":
    playing = False
    print("Thank you for playing")

The Code Explained

You have probably noticed that we have to run the program every time we want to play a round. What if we want to play multiple rounds and keep track of the score? We can change our code to do just that! This is calledΒ refactoringΒ and programmers do it all the time to add new functionality to their code and to clean it up.

We need to create aΒ variableΒ to let our game know that we want to keep playing!

🌟Keep in mind to capitalize your True and False (Boolean) statements otherwise the code will not work.

playing = True

Now that we have these three variables set up, we can introduce theΒ while loop. A while loop will continuously run through code while a value is true.

Think of it like your refrigerator. While the fridge door is open, the light stays on. Does the light stay on if the door is closed?

To use a while loop we need to put all over our previous code inside it, like an umbrella. Do this by tabbing each line of code.

  • All of the player_wins/comp_wins/prints should line up.
  • All of the if/elif statements should line up with each other.

This will allow our previous code to run forever. If you start the game now, you will see that the game loops forever. We aren’t finished yet though!Β 

Now we need to display that score after every match. To do this, we will add code inside of our while loop but outside of our last conditional! It should look something like this:

print("Player Wins: " + str(player_wins) + " - Computer Wins: " + str(comp_wins))

This line will display how many wins you have and how many wins the computer has. You may notice that this line has plus signs in it. This allows us to glue multiple pieces of texts and variables together into one line and is known asΒ concatenation. We also have to use the str() function to change our numbers into strings because that is the only way they can be concatenated!

Now we simply need to add a few more lines of code that will allow the user to quit the game if they want. We should now ask if the player wants to quit and let them quit if they do. The following code will help us do that:

 stop = ""

  while stop not in ["y", "n"]:
    stop = input("Would you like to stop playing? [y]/[n]?: ")

  if stop == "y":
    playing = False
    print("Thank you for playing")

After adding this code, you will be able to run your game and see your creation!