0

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}" )
       
3
  • You enter the while loop because not the user_score nor the computer_score are 2 and then you for 100 times (no matter what). Commented Feb 12, 2023 at 18:33
  • Try using an if inside the for loop instead of the while outside. Commented Feb 12, 2023 at 18:34
  • 1
    And you should loop while user_score != 2 and computer_score != 2 Commented Feb 12, 2023 at 18:36

2 Answers 2

2
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.

Sign up to request clarification or add additional context in comments.

1 Comment

Hey John, thank you so much, I'm completely new to this, and I'm still learning, but what you said really helped me out. Thank you.
0

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()

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.