Eric Mirzabekov
Eric Mirzabekov

Reputation: 25

Is there a way to make a while loop repeat itself?

I am trying to make this guessing game using rudimentary python. It looks like this:

secret_number = 9
guess_count = 0
guess_limit = 3

while guess_count < 3:
    guess_count += 1
    guess = int(input('Guess: '))
    if guess == secret_number:
        print('You got it!')

I want to set it up so it asks the user if they want to play again, and if it is yes, repeat the while loop. How would I go by this?

Upvotes: 0

Views: 691

Answers (4)

malek
malek

Reputation: 9

Some conditions are missing

secret_number = 9
guess_count = 0
guess_limit = 3
isEnded = False
while not isEnded:
    while guess_count < 3:
        guess_count += 1
        guess = int(input('Guess: '))
        if guess == secret_number:
            guess_count = 3
            print('You got it!')

    if input("Play again? --> Yes/No \n") == "No":
        isEnded = True
    else:
        guess_count = 0

Upvotes: 0

Lukasz Wiecek
Lukasz Wiecek

Reputation: 424

I would use Game class to decouple game logic from user input like this:

import random

class Game:
    def __init__(self, guess_limit=3):
        self.guess_limit = guess_limit
        self.secret_number = random.randint(1, 9)
        self.guesses = 0
        self.last_guess = None

    @property
    def over(self):
        return self.won or self.lost

    @property
    def won(self):
        return self.last_guess == self.secret_number

    @property
    def lost(self):
        return self.guesses >= self.guess_limit

    def guess(self, number):
        self.last_guess = number
        self.guesses += 1
    
def main():
    while True:
        game = Game()
        while not game.over:
            number = int(input("Guess: "))
            game.guess(number)

        if game.won:
            print("Congratulations!")
        else:
            print("Game over. You lost.")

        if input("Try again (y/n)? ") != 'y':
            print("Bye!")
            break

main()

I know it's a bit verbose but wanted to give OOP a try here :)

Upvotes: 0

3therk1ll
3therk1ll

Reputation: 2421

You could either place the whole thing in a while loop or create a function such as below and call the function if your repeat condition is met. This has the added bonus of your being able to call this part of the game from other parts of your code.

def game():
    secret_number = 9
    guess_count = 0
    guess_limit = 3

    while guess_count < 3:
        guess_count += 1
        guess = int(input('Guess: '))
        if guess == secret_number:
            print('You got it!')
    if some_condtion_like_user_input is True:
        game()

If you want to avoid recursion, something like the below:

def game():

    finish = False

    while not finish:
        secret_number = 9
        guess_count = 0
        while guess_count < 3:
            guess_count += 1
            guess = int(input('Guess: '))
            if guess == secret_number:
                print('You got it!')

        repeat = input("Start again? Y/N: ")
        if repeat == "N":
            finish = True

if __name__ == "__main__":
    game()

Upvotes: 1

Fran Arenas
Fran Arenas

Reputation: 648

Use another loop:

secret_number = 9
guess_limit = 3

end = False

while not end:
    for _ in range(guess_limit): # Using for range() is more pythonic
        guess = int(input('Guess: '))
        if guess == secret_number:
            print('You got it!')
            break
    
    if input("Play again? --> Yes/No") == "No":
        end = True

Upvotes: 1

Related Questions