1
import random

while True:
    score_player = 0
    score_computer = 0

    while score_computer <= 2 or score_player <= 2:
        choices = ['rock', 'scissors', 'paper']
        player = None
        computer = random.choice(choices)

        while player not in choices:
            player = input('rock,scissors or paper? : ').lower()

        print('computer:', computer)
        print('player: ', player)

        if player == computer:
            print('You are tied!')
        elif player == 'rock':
            if computer == 'scissors':
                score_player += 1
            elif computer == 'paper':
                score_computer += 1

        elif player == 'scissors':
            if computer == 'paper':
                score_player += 1
            elif computer == 'rock':
                score_computer += 1

        elif player == 'paper':
            if computer == 'rock':
                score_player += 1
            elif computer == 'scissors':
                score_computer += 1

        print('Score_player : ', score_player)
        print('Score_computer', score_computer)


    playe_again = input('Do you want to continue? (Yes/No) :').lower()
    #while playe_again == 'yes' or 'no':
    if playe_again != 'yes':
        break
print('I hope you have enjoyed the game!')

Why the conditions inside while loop are not executed correctly.
I want the game to stop when the score of one of them reaches 3, But it continues until both of them reach 3, and one of them may even become more than 3.

1 Answers1

1

The loop condition of

while score_computer <= 2 or score_player <= 2

is the problem.

It will loop as long as either one of the conditions is true.

So if score_player keeps at zero (for example) then the value of score_computer can go up quite a lot.

You need to use and for the condition:

while score_computer <= 2 and score_player <= 2

Then as soon as one score reaches 3 the whole condition will become false, and the loop end.


Some repetition of the basic logic operators:

  • False or False is False
  • True or False, False or True, True or True are all True
  • False and False, False and True, True and False are all False
  • True and True is True
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • I tested it with 'and' but it still doesn't work.(The same problem) – user21743456 Apr 26 '23 at 14:05
  • @user21743456 Instead of using random number for testing, use a fixed set of value that you know will cause the problem. Then use a [*debugger*](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) to step through the code line by line to see what happens. And please *edit* your question to give us more details, because if you "want the game to stop when the score of one of them reaches 3" then the loop condition issue in my answer should fix that (if one reaches a score of `3` the loop should end). – Some programmer dude Apr 26 '23 at 14:18