Python Forum
Trying to implement Best of 3 logic in rock paper scissors game
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to implement Best of 3 logic in rock paper scissors game
#1
Hi

I've written a rock paper scissors game now want to code in 'Best of 3' logic. Right now, it plays 3 times and then exits.

Any advice will be appreciated. Thanks.

mychoice = []
for i in range(3):
  mychoice = input("Please enter your choice of rock, paper or scissors?\n").lower()
  while mychoice not in ["rock","paper","scissors"]:
    print("Please spell correctly.\n")
    mychoice = input("Please enter your choice of rock, paper or scissors?\n").lower()
  list = ["rock","paper","scissors"]
  import random
  compchoice = random.choice(list)
  print("Computer chose" + " - " + compchoice)
  if mychoice == compchoice:
    print("We chose the same things. Let's play again.\n")
    break  
  if mychoice == 'rock' and compchoice == 'scissors':
    Result1 = "You win.\n"
    print(Result1)
  elif mychoice == 'rock' and compchoice == 'paper':
    Result2 = "I win.\n"
    print(Result2)
  if mychoice == 'paper' and compchoice == 'scissors':
    Result2 = "I win.\n"
    print(Result2)
  elif mychoice == 'paper' and compchoice == 'rock':
    Result1 = "You win.\n" 
    print(Result1)
  if mychoice == "Scissors" and compchoice == 'paper':
    Result1 = "You win.\n"
    print(Result1)
  elif mychoice == "Scissors" and compchoice == 'rock':
    Result2 = "I win.\n"
    print(Result2)
    break
Reply
#2
i haven't been able to run your code
but i think the following runs a lot neater alrady

import random

mychoice = []
for i in range(3):
    mychoice = input("Please enter your choice of rock, paper or scissors?\n").lower()
while mychoice not in ["rock", "paper", "scissors"]:
    print("Please spell correctly.\n")
mychoice = input("Please enter your choice of rock, paper or scissors?\n").lower()
list = ["rock", "paper", "scissors"]


compchoice = random.choice(list)
print("Computer chose" + " - " + compchoice)
if mychoice == compchoice:
    result = "We chose the same things. Let's play again."
elif mychoice == 'rock' and compchoice == 'scissors':
    Result = "You win."
elif mychoice == 'rock' and compchoice == 'paper':
    Result = "I win."
if mychoice == 'paper' and compchoice == 'scissors':
    Result = "I win."
elif mychoice == 'paper' and compchoice == 'rock':
    Result = "You win."
if mychoice == "Scissors" and compchoice == 'paper':
    Result = "You win."
elif mychoice == "Scissors" and compchoice == 'rock':
    Result = "I win."
print(Result + r"\n")
ps:
please put your code between the correct tags next time
Reply
#3
Keep track of how many wins each player has. Instead of playing exactly 3 games, play until one of the players has 2 wins.
Reply
#4
Thanks guys.
I couldn't run your code either keuninkske. Copied/Pasted it but it didn't work.

@bowlofred: Could you please help me understand how I'd implement the logic? Thanks.

@bowlofred I stored the results in variables.
Reply
#5
Something like:

p1_wins = 0
p2_wins = 0
games_played = 0
while max([p1_wins, p2_wins]) < 2:
    # play game here
    # if p1 wins:
        p1_wins += 1
    # if p2 wins:
        p2_wins += 1
    games_played += 1

# now print your end-of-game stuff.  You can show who won and how many games were played.
Reply
#6
Thanks. I'll try.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Rock Paper Scissors Project in Python ankitdixit 8 4,816 Feb-23-2024, 03:14 PM
Last Post: DPaul
  Trying to create a visual rock paper scissors game urmom33 1 1,027 Dec-03-2022, 09:12 PM
Last Post: deanhystad
  Right way to implement interfaces yossiy123 1 1,272 May-12-2022, 10:31 AM
Last Post: Gribouillis
  Rock paper scissors in python with "algorithm" Agat0 23 5,995 Mar-01-2022, 03:20 PM
Last Post: Agat0
  Problem restricting user input in my rock paper scissors game ashergreen 6 4,590 Mar-25-2021, 03:54 AM
Last Post: deanhystad
Question best way to implement algorithm hamidze 3 2,210 Feb-27-2021, 07:10 PM
Last Post: hamidze
  Odd behavior with Rock Paper Scissor game DustinKlent 2 1,932 Aug-27-2020, 03:55 PM
Last Post: DustinKlent
  Rock Paper Scissors with dictionaries ewgreht 2 3,886 May-01-2020, 03:19 PM
Last Post: deanhystad
  I haven't a clue how to implement this logic 357mag 3 2,108 Apr-02-2020, 04:35 PM
Last Post: 357mag
  Rock, Paper, Scissors.. Help..hidden bug xxunknownxx 4 2,659 Mar-19-2020, 02:46 AM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020