I need help in my while loop conditions in python

Question:

I created a game, in which there is a secret number, and you guess, then the computer guesses. The secret number changes every round. To win, either you or the computer needs to get a score of 2.
I don’t know how many rounds it will take, so I loop the game 100 times in a while loop with the condition that the user_Score and computer_Score is not equal to 2,
however, when I run my code, it seems like it just skips the condition, and goes on 100 times, and I don’t understand why

while user_score != 2 or computer_score != 2:
    for _ in range(100):
        game()

The above code is the part which I am talking about, it is in the near bottom of my code

import random

computer_score = 0
user_score = 0 
    
def game():
    global user_score
    global computer_score
   
    secret_number = random.randrange(10)
    while True:
        user_guess = input("Guess a number from 0 - 10:  ")
        if user_guess.isdigit():
            user_guess = int(user_guess)
            if 0 <= user_guess <= 10:
                break
            else:
                print("Enter a valid number from 0 - 10")
        else:
            print("Please enter a valid number from 0 - 10")
    computer_guess = random.randrange(10)
    
    if user_guess == secret_number:
        print("Hooray! You guessed correctly")
        user_score += 1
        print(f"Your score is {user_score}")
    else:
        print("Sorry wrong guess")
    print("Now Player 2's turn")
    
    if computer_guess == secret_number:
        print("Player 2 guessed the correct number!")
        computer_score += 1
        print(f"Player 2's score is {computer_score}")
    else:
        print("Player 2 guesed incorrectly")
    
    print( f" Your score: {user_score}" + "   " + f"Player 2 score: {computer_score}")
game() 
 
 
while user_score != 2 or computer_score != 2:
    for _ in range(100):
        game()

if user_score == 2:
    print(f"Hooray! You won with a score of {user_score} points")
    print(f"Player 2 got {computer_score} points")
elif computer_score == 2:
    print(f"Sorry, you lost. Your score is {user_score}, and Player 2 got {computer_score} points")
print( f" Your final score: {user_score}" + "   " + f"Player 2 final score: {computer_score}" )
       
Asked By: Aryan Singh

||

Answers:

while user_score != 2 or computer_score != 2:
    for _ in range(100):
        game()

If a player gets a score of two, that will not stop the inner for loop. It’s going to loop to 100, because that’s what you told it to do.

I’m confused why you need the inner loop at all, though. Isn’t the outer while loop enough?

while user_score != 2 or computer_score != 2:
    game()

Also, wouldn’t you want to use and here, instead of or? The way this is written, it will only stop if both scores are equal to 2. And if one of the players gets to a score of 3 while the other player has a score of 0 or 1, this loop will never exit.

Answered By: John Gordon

It will always show this same fault. Because, at the beginning of the game when 1st round is over, the for loop starts iterating and it will continue for 100 times. Best way is to remove the for loop and just run the while loop.

while user_score != 2 or computer_score != 2:
    game()
Answered By: Rajarshi Paul
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.