Home > Blockchain >  Rock, paper, scissors game in Python - some clarification needed
Rock, paper, scissors game in Python - some clarification needed

Time:12-18

I am writing a simple game of rock, paper and scissors, where the user competes with a computer. There are 5 tries. The problems I ran into are :

  1. If I enter something apart from three options, it should return "Invalid Entry". Instead, the program stops
  2. The program never prints "You won the game" or "You lost the game", it finishes after 5 attempts

Other feedback would also be appreciated

import random

def game():
  win_count = 0
  loose_count = 0
  tries = 0
  while tries < 5:
    chosen = input("Make your choice: ")
    if chosen == "scissors" or chosen == "Scissors":
      element = "scissors"
    elif chosen == "paper" or chosen == "Paper":
      element = "paper"
    elif chosen == "rock" or chosen == "Rock":
      element = "rock"
    else:
      return "Invalid Entry"

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

    if element == "scissors" and computer_choice == "paper":
      print("Computer chose paper, you chose scissors, you win !")
      win_count  = 1
      tries  = 1
    elif element == "paper" and computer_choice == "scissors":
      print("Computer chose scissors, you chose paper, you loose !")
      loose_count  = 1
      tries  = 1
    elif element == "paper" and computer_choice == "rock":
      print("Computer chose rock, you chose paper, you win !")
      win_count  = 1
      tries  = 1
    elif element == "rock" and computer_choice == "paper":
      print("Computer chose paper, you chose rock, you loose !")
      loose_count  = 1
      tries  = 1
    else:
      print("Whoops, that's a draw, try again")
      tries =1

  print("Your Wins: "  str(win_count))
  print("Computer Wins: " str(loose_count))
  if win_count > loose_count:
    return "Congrats, you won the game!"
  else:
    return "Sorry, you lost"
game()

CodePudding user response:

return statement does not print anything but they return the value from a function and as soon as a return statement executes the function ends up executing that's why whenever the user inputs something invalid the program stop.

Also not use the if statement from if chosen = paper or chosen = Paper instead use .lower() to lower the string.

I have made some changes to your code. Try this code

import random

def game():
  win_count = 0
  loose_count = 0
  tries = 0
  while tries < 5:
    element = input("Make your choice: ").lower()
    

    computer_choices = ["scissors", "paper", "rock"]
    if element not in computer_choices:
        print("Invalid choice")
        continue

    computer_choice = random.choice(computer_choices)

    if element == "scissors" and computer_choice == "paper":
      print("Computer chose paper, you chose scissors, you win !")
      win_count  = 1
      tries  = 1
    elif element == "paper" and computer_choice == "scissors":
      print("Computer chose scissors, you chose paper, you loose !")
      loose_count  = 1
      tries  = 1
    elif element == "paper" and computer_choice == "rock":
      print("Computer chose rock, you chose paper, you win !")
      win_count  = 1
      tries  = 1
    elif element == "rock" and computer_choice == "paper":
      print("Computer chose paper, you chose rock, you loose !")
      loose_count  = 1
      tries  = 1
    else:
      print("Whoops, that's a draw, try again")
      tries =1

  print("Your Wins: "  str(win_count))
  print("Computer Wins: " str(loose_count))
  if win_count > loose_count:
    print("Congrats, you won the game!")
  else:
    print("Sorry, you lost")
game()

  • Related