Having trouble storing the results of a rock paper scissors game

Viewed 43

I am unable to make the timesPlayed increase and the wins,draws and defeats. I want to be able to calculate the win percentage when the user is finished playing. This is my code

import random
play = input("Do you want to play a game of Rock Paper Scissors? (yes/no)")
while play == "yes":
    global draws, wins, defeats, timesPlayed
    draws, wins, defeats, timesPlayed = 0, 0, 0, 0
    timesPlayed = timesPlayed + 1
    rps = ["rock", "paper", "scissors"]
    compChoice = random.choice(rps)

    choice = input("Enter your choice: ")
    print("You chose " + choice)
    if choice.lower() == compChoice:
        print("Computer chose " + compChoice)
        print("Game is a draw")
        draws += 1

    elif choice.lower() == "rock" and compChoice == "paper":
        print("Computer chose " + compChoice)
        print("You lose!")
        defeats += 1

    elif choice.lower() == "scissors" and compChoice == "rock":
        print("Computer chose " + compChoice)
        print("You lose!")
        defeats += 1

    elif choice.lower() == "paper" and compChoice == "scissors":
        print("Computer chose " + compChoice)
        print("You lose!")
        defeats += 1

    elif choice.lower() != "paper" and choice.lower() != "rock" and choice.lower() != "scissors":
        print("Play properly!")

    else:
        print("Computer chose " + compChoice)
        print("You win!")
        wins += 1


    print("Do you want to play again? (yes/no)")
    playAgain = input()
    if playAgain == "no":
        print("Thank you for playing")
        txt = "You won {} out of the {} games you played"
        print(txt.format(wins, timesPlayed))
        break
2 Answers

Don't redefine your variables at the start of your while loop: move them outside.

draws, wins, defeats, timesPlayed = 0, 0, 0, 0
while play == "yes":
    #code omitted for brevity

You are resetting draws, wins, defeats, timesPlayed to 0 each time the loop iterates.

If you want to have them not reset on each game, move this line to before the loop:

draws, wins, defeats, timesPlayed = 0, 0, 0, 0
Related