1

How to code your first Rock, Paper, Scissors, Lizard and Spock game c:

Have you ever found yourself longing to play Rock, Paper, Scissors, Lizard, and Spock, but you don’t have friends to play with? That’s no longer an issue. Let’s code a game that allows you to play anytime you want!

Step One Let’s lay the foundation for our game by importing the random module from Python and getting to know who will play against the computer.

import random

print("Welcome to Rock, Paper, Scissors, Lizard, Spock")print("-" * 48)
user = input("Add your username => ")

Step Two Add variables to keep track of the progress of both the user and computer, as well as the number of rounds.

tie = 0user_win = 0computer_win = 0rounds = 1

Step Three Define a loop that continues until the user or computer wins three out of five rounds.

while user_win < 3and computer_win < 3:

Step Four Display rounds information

print("*" * 30)
  print("ROUND", rounds)

Step Five Ask the user to choose their weapon (rock, paper, scissors, lizard, or spock) and ensure it is valid.

  user_choice = input("Choose your weapon => ").lower()
  while user_choice not in ["rock", "paper", "scissors", "lizard", "spock"]:
    print("Invalid choice, please chose Rock, Paper, Scissors, Lizard or Spock")
    user_choice = input("Choose your weapon =>").lower()

Step Six Do you remember that we imported the random module at the start? Let’s generate a random choice for the computer using it.

  computer_choice = random.choice(["rock", "paper", "scissors", "lizard", "spock"])

Step Seven Now, let’s define a directory of the possible outcomes for each combination of choices that specifies what happens when one choice is made against the other.

  outcomes = {
    "rock" : {"scissors" : "crushes", "lizard" : "crushes"},
    "paper" : {"rock" : "covers", "spock" : "disproves"},
    "scissors" : {"paper" : "cuts", "lizard" : "decapitates"},
    "lizard" : {"paper" : "eats", "spock" : "poisons"},
    "spock" : {"rock" : "vaporizes", "scissors" : "smash"}
  }

Step Eight We’re almost done c: We need to determine a winner for each round, add those results to the previously established counters, and display the score.

if user_choice == computer_choice:
    print("Tie! Both players chose", user_choice)
    tie += 1
  elif computer_choice in outcomes[user_choice]:
     print(f"{user_choice.capitalize()} {outcomes[user_choice][computer_choice]} {computer_choice.capitalize()} {user} wins!")
     user_win += 1
  else:
    print(f"{computer_choice.capitalize()} {outcomes[computer_choice][user_choice]} {user_choice.capitalize()} {user} loses!")
    computer_win += 1
  print(f"Score: {user} {user_win} - {computer_win} Computer")
  rounds += 1

Step Nine That was it, now we just need to display the end-game result and overall winner.

print("Thanks for playing!")
if user_win > computer_win:
  print(f"{user} wins the game!")
else:
  print("Computer wins the game!")

Please comment on any suggestions to improve or streamline the code. ty c:

Escribe tu comentario
+ 2
1
10 meses

It can hardly be said that there is no connection between games and technology. The better and faster technology develops, the better the games become. By the way, this also applies to online casinos. I am sure that you can find a lot of articles and information about online gambling and modern technologies on the Internet. And this is a rather interesting topic; you can see how the gambling industry has developed.