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...
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)