0

I am creating a python code for rock, paper, scissors but I can't seem to keep track of the score of wins, losses, and ties. I think there is something wrong with my "count" which i called "win += 0", "lose += 0" and "tie += 0." Could someone please tell me what I should do to increase the score by 1 every time there is a win, lose or tie?

Here is my code below:

from random import randint

t = ["rock", "paper", "scissors"]
computer = t[randint(0,2)]
player = False
lose = 0
win = 0

for i in range(0,10):
    print("1... 2... 3... go!")
    while player == False:
        player = input("rock, paper, scissors: ")
        print("Computer: ", computer)
        print("User: ", player)
        if player == computer:
            tie = 0
            tie +=1
            print("Tie!")
        elif player == "rock":
            if computer == "paper":
                lose +=0
                print("You lose!")
            else:
                win +=0
                print("You win!")
        elif player == "paper":
            if computer == "scissors":
                lose = 0
                lose +=0
                print("You lose!")
            else:
                win +=0
                print("You win!")
        elif player == "scissors":
            if computer == "rock":
                lose +=0
                print("You lose!")
            else:
                win +=0
                print("You win!")
        else:
            print("That's not a valid play. Check your spelling!")

        player = False
        computer = t[randint(0,2)]
        break
print("Final Tally")
print("************")
print("User Wins: ", win)
print("Computer Wins: ", lose)
print("Ties: ", tie)

if tie > win and tie > lose:
    print("It's a tie!")
elif win > tie and win > lose:
    print("You won!")
else:
    print("The computer won!")
6
  • 3
    tie = 0 should be before the loop, not every time there's a tie. Commented Jun 4, 2020 at 17:50
  • 3
    variable += 0 doesn't change the variable. If you want to add 1, it should be += 1 Commented Jun 4, 2020 at 17:50
  • 1
    If you want to increment a variable in this way, use win += 1 which is the equivalent to win = win + 1 with an int Commented Jun 4, 2020 at 17:50
  • 1
    FYI, computer = random.choice(t). Commented Jun 4, 2020 at 17:53
  • Try this, you'll like it: from random import choice t = ["rock", "paper", "scissors"] computer = choice(t) Commented Jun 4, 2020 at 17:58

1 Answer 1

1

Here's the fixed version. I suggest you work on it some more :)

from random import choice

t = ["rock", "paper", "scissors"]

tie = 0
lose = 0
win = 0

for i in range(0, 10):
    print("1... 2... 3... go!")

    # you need to refresh these variables on every for iteration
    computer = choice(t)
    player = None

    # if you're using while to make sure player inputs, that's the only thing that needs
    # to be within the while loop
    while not player:
        player = input("rock, paper, scissors: ")
    print("Computer: ", computer)
    print("User: ", player)

    # I would look for a way to simplify determining the winner
    if player == computer:
        # tie += 1 is the same as tie = tie + 1
        tie +=1
        print("Tie!")
    elif player == "rock":
        if computer == "paper":
            lose += 1
            print("You lose!")
        else:
            win += 1
            print("You win!")
    elif player == "paper":
        if computer == "scissors":
            lose += 1
            print("You lose!")
        else:
            win += 1
            print("You win!")
    elif player == "scissors":
        if computer == "rock":
            lose += 1
            print("You lose!")
        else:
            win += 1
            print("You win!")
    else:
        print("That's not a valid play. Check your spelling!")


print("Final Tally")
print("************")
print("User Wins: ", win)
print("Computer Wins: ", lose)
print("Ties: ", tie)

if tie > win and tie > lose:
    print("It's a tie!")
elif win > tie and win > lose:
    print("You won!")
else:
    print("The computer won!")

UPDATE: Apparently I have nothing to do. So ok, here's a straightforward way to simplify win conditioning.

    win_condition_rock = player == 'rock' and computer == 'scissors'
    win_condition_paper = player == 'paper' and computer == 'rock'
    win_condition_scissors = player == 'scissors' and computer == 'paper'

    if player == computer:
        # tie += 1 is the same as tie = tie + 1
        tie +=1
        print("Tie!")

    elif any([win_condition_paper, win_condition_scissors, win_condition_rock]):
        win += 1
        print('You win')

    else:
        lose += 1
        print('You lose')

UPDATE 2: And here's a check for valid input

    while player not in t:
        player = input("rock, paper, scissors: ").lower()
        if player not in t:
            print('Invalid input')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.