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}" )
whileloop because not theuser_scorenor thecomputer_scoreare 2 and then youfor100 times (no matter what).ifinside theforloop instead of thewhileoutside.user_score != 2andcomputer_score != 2