mrpopsicle
mrpopsicle

Reputation: 13

python counting score in a loop

I made a rock,paper scissors game which works fine, but I would like to add a score for both the user and AI, counting how many times each has won. I tried dedicating a pair of score variables and and then adding 1 to it each time but it keeps giving 0. I kept the increments in the first if statement to show what I mean. I also tried the string.count and it didn't work. Any help would be appreciated.

import sys
from random import randint
import math
choice1=input("would you like to play a game of rock,paper and scissors: ")
while choice1=="yes":
    choice2=input("please choose rock,paper or scissor: ")
    computer=randint(1,3)
    score=0
    score2=0
    if choice2 in ("rock","paper","scissor"):
        if computer==1:
            print("You have chosen "+ choice2 + " and the computer has chosen rock ")
            if choice2=="rock":
                print("It'/s a draw ")
            elif choice2=="paper":
                print("You win! ")
                score + 1
            elif choice2=="scissor":
                print("You lose :( ")
                score2 + 
            else:
                sys.exit()
        elif computer==2:
            print("You have chosen " + choice2 + " and the computer has chosen paper ")
            if choice2=="rock":
                print("You lost :( ")
            elif choice2=="paper":
                print("It'/s a draw")
            elif choice2=="scissor":
                print("You won! ")
            else:
                sys.exit()
        elif computer==3:
            print("You have chosen " + choice2 + " and the computer has chosen scissor ")
            if choice2=="rock":
                print("You won! ")
            elif choice2=="paper":
                print("you lost :( ")
            elif choice2=="scissor":
                print("It'/s a draw ")
            else:
                sys.exit()
        choice3=input("Would you like to play again? Type yes or no: ")
        if choice3=="no":
            print (score)
            print (score2)
            sys.exit()
    else:
        sys.exit()
else:
    print("GoodBye")
    sys.exit()

Upvotes: 0

Views: 3454

Answers (2)

Filip Wojciechowski
Filip Wojciechowski

Reputation: 349

To avoid the values of score and score1 being reset to 0 on every iteration of the loop you should initialize both variables above the while loop.

Then you should increment the values by using

score += 1

and

score1 += 1

Upvotes: 0

Prune
Prune

Reputation: 77880

You increment the score, but fail to store the value. Try

score += 1
...
score2 += 1

Also, set the scores to 0 before the while loop: just once at the start of the game. You reset them in each round.

Upvotes: 2

Related Questions