-1

So, I created a code that simulates the rock, paper, scissors game. When I first open the program, it works! Fresh from the start, it works.

The problem is when there is a second run. For some reason, my "while" doesnt work anymore. The while loop should stop when any player reaches 3 wins. Yo can see that I reached 17 and 13 wins and the thing doesnt stop...

enter image description here

This is happening only on the post-first-runs of the program.

Code:

###########function of the random module
import random

###########possible options of play and the number of single wins to win the match
options = ["stone", "paper", "scissors"]
win_the_game = 3

############function that randomly returns one of the 3 options

def machine_play(a):
    return(random.choice(a))

############function that asks your choice: 'stone', 'paper' or 'scissors'

def user_play():
    a = input("Enter your choice: ")
    if a not in options:
            print("Not a valid option, try again ('stone', 'paper', 'scissors')")
    while a not in options:
        a = input("Enter your choice: ")
        if a not in options:
            print("Not a valid option, try again ('stone', 'paper', 'scissors')")
    return a

############function that resolves a combat

def combat(mchoice,uchoice):
    if mchoice == uchoice:
        return 0
    elif uchoice == "stone":
        if mchoice == "scissors":
            return 2
        else:
            return 1
    elif uchoice == "paper":
        if mchoice == "stone":
            return 2
        else:
            return 1
    elif uchoice == "scissors":
        if mchoice == "paper":
            return 2
        else:
            return 1

############variables that accumulate the wins of each participant
machine_wins = 0
user_wins = 0

#######################     The final loop or program     ######################
while machine_wins or user_wins < win_the_game:
    user_choice = user_play()
    machine_choice = random.choice(options)
    print("The machine choice is: ", machine_choice)

    the_game = combat(machine_choice, user_choice)
    if the_game == 1:
        machine_wins += 1
    elif the_game == 2:
        user_wins += 1
    else:
        print("Its a tie!! continue")

    print("\nUser wins: ", user_wins, "\nMachine wins: ", machine_wins) 

1 Answers1

4
while machine_wins or user_wins < win_the_game:

This parses as

while machine_wins or (user_wins < win_the_game):

As long as machine_wins is "truthy", i.e. positive, the game will continue forever. I believe that what you need is

while machine_wins < win_the_game and \
         user_wins < win_the_game:
Prune
  • 76,765
  • 14
  • 60
  • 81
  • I _think_ as soon as the machine _or_ the user wins 3 games it is supposed to quit, in which case you'd still want `or`. – Alexander Nied Oct 17 '19 at 18:13
  • @Alexander But if one scores 3 first the game will continue cause one of the expressions still is less than win_the_game, so that's why you need to use AND. – gpaulini Oct 17 '19 at 18:22
  • @AlexanderNied: yes, it's supposed to quit in that case. However, the statement logic is for *remaining* in the game. See DeMorgan's theorems. – Prune Oct 17 '19 at 18:34
  • The AND worked fine, the program runs anytime smooth now. Still don't understand why when I firts open the program from scratch, works fine only one time with the OR – Luis Fernando López Corrales Oct 17 '19 at 19:21
  • The only situation in which the old code should work is when the user wins 3-0. – Prune Oct 17 '19 at 19:26