meffin
meffin

Reputation: 21

Why is the while loop not stopping

I'm new to programming and decided to try and make a simple best of 3 rock paper scissors game in which you play against the computer. But when I test the code, even after me or the computer gets 2 wins the while loop doesnt stop.

import random

computerInputList = ["rock", "paper", "scissors"]
userScore = 0
computerScore = 0

while userScore < 2 or computerScore < 2:
    computerInput = random.choice(computerInputList)
    print("rock, paper or scissors? ")
    userInput = input()
    print()

    if userInput == computerInput:
        print("You played: " + userInput)
        print("Computer played: " + computerInput)
        print()
        print("This round is a tie!")

    elif userInput != computerInput:
        if userInput == "rock":
            if computerInput == "scissors":
                print("You played: " + userInput)
                print("Computer played: " + computerInput)
                print()
                print("You won this round!")
                userScore = userScore + 1
            elif computerInput == "paper":
                print("You played: " + userInput)
                print("Computer played: " + computerInput)
                print()
                print("You lost this round!")
                computerScore = computerScore + 1
        elif userInput == "paper":
            if computerInput == "rock":
                print("You played: " + userInput)
                print("Computer played: " + computerInput)
                print()
                print("You won this round!")
                userScore = userScore + 1
            elif computerInput == "scissors":
                print("You played: " + userInput)
                print("Computer played: " + computerInput)
                print()
                print("You lost this round!")
                computerScore = computerScore + 1
        elif userInput == "scissors":
            if computerInput == "paper":
                print("You played: " + userInput)
                print("Computer played: " + computerInput)
                print()
                print("You won this round!")
                userScore = userScore + 1
            elif computerInput == "rock":
                print("You played: " + userInput)
                print("Computer played: " + computerInput)
                print()
                print("You lost this round!")
                computerScore = computerScore + 1
        else:
            print("Invalid input!")
            print("Accepted inputs: rock, paper, scissors.")

I get no error messages

Upvotes: 0

Views: 62

Answers (2)

Lior Cohen
Lior Cohen

Reputation: 5755

you should have a and in the while instead of the or.

You want to continue when both are below 2.

Upvotes: 0

esqew
esqew

Reputation: 44711

It's because you have a fundamental misunderstanding of the way the or operator works in this situation:

while userScore < 2 or computerScore < 2:

The loop contained in this while block will continue to execute if either userScore or computerScore is less than 2.

What you're looking for is for the loop to end when one of the two exceeds 2. Change your or to and to ensure that the loop will only continue when both userScore and computerScore are less than 2:

while userScore < 2 and computerScore < 2:

Upvotes: 5

Related Questions