Python Forum
Trying to implement Best of 3 logic in rock paper scissors game - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Trying to implement Best of 3 logic in rock paper scissors game (/thread-26706.html)



Trying to implement Best of 3 logic in rock paper scissors game - ShAhCh - May-10-2020

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



RE: Trying to implement Best of 3 logic in rock paper scissors game - keuninkske - May-10-2020

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


RE: Trying to implement Best of 3 logic in rock paper scissors game - bowlofred - May-10-2020

Keep track of how many wins each player has. Instead of playing exactly 3 games, play until one of the players has 2 wins.


RE: Trying to implement Best of 3 logic in rock paper scissors game - ShAhCh - May-10-2020

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.


RE: Trying to implement Best of 3 logic in rock paper scissors game - bowlofred - May-10-2020

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.



RE: Trying to implement Best of 3 logic in rock paper scissors game - ShAhCh - May-11-2020

Thanks. I'll try.