1
\$\begingroup\$

This is the second version of my lotto game, based on this thread before. I hope I used all the tips I was given, also I fixed it using only functions as promised.

Game rules

Each player is given 15 numbers, in increasing order. This is why I use sorted() on the lists of player and computer. Also there is a common list with numbers in it from 1 to 90.

A random number is picked and removed from this list and printed to players. The player who has this number in his card crosses it out or skips its turn. This continues until either player or computer crosses out all his numbers, or for some strange reason they are out of numbers. I think in other parts of the world it is also called BINGO but I might be wrong.

import random
import os

number_pool = list(range(1, 11))  # 1 , 101
player_card = sorted(random.sample(number_pool, 3))  # 15
computer_card = sorted(random.sample(number_pool, 3))  # 15


def random_choice(num_pool):
    choice = random.choice(num_pool)
    num_pool.remove(choice)
    # print("Current lotto: " + str(choice))
    return choice


def game_status_check():
    if len(player_card) == 0 or len(computer_card) == 0:
        print("Game over !")
        return True
    else:
        return False


def printout_cards():
    print("######################################\n")
    print("Player card: " + str(player_card))
    print("\n######################################\n")
    print("Computer card: " + str(computer_card))


def end_game():
    answer = input("Do you want to restart the game [y or n] ?: ")
    if answer == "y":
        main()
    elif answer == "n":
        print("Thank you for playing ! ")
        exit()
    else:
        print("Enter y or n !")


def main():
    while not game_status_check():
        printout_cards()
        random_number = random_choice(number_pool)
        print("Random lotto is: {}".format(random_number))
        answer = input("Do you want to cross out number [y or n]: ")
        if answer == "y":
            player_card.remove(random_number)
            if random_number in computer_card:
                computer_card.remove(random_number)
        elif answer == "n":
            if random_number in player_card:
                print("The number was in your card, you missed it and lost !")
                end_game()
            elif random_number in computer_card:
                computer_card.remove(random_number)
        os.system("cls")
        game_status_check()


if __name__ == '__main__':
    main()
\$\endgroup\$
1
  • \$\begingroup\$ also i shortened the numbers to be able to test more hastily \$\endgroup\$ Jul 13, 2017 at 14:24

1 Answer 1

1
\$\begingroup\$

For a basic program like this, everything is more or less okay. Here are some nits:

  1. game_status_check() should only do checking. Put the print("Game over !") in your main function.

    def game_status_check():
        return len(player_card) == 0 or len(computer_card) == 0
    

    This also removes the need for you to do:

    while not game_status_check():
        ...
        game_status_check()
    
  2. What happens if a player doesn't remove a card that was in their set of player cards, and then enters an answer that's not y or n? Your end_game() function has a handler for that, but doesn't prompt the user again for input. Suggestion:

    def end_game():
        answer = input("Do you want to restart the game [y or n] ?: ")
        while answer != "y" and answer != "n":
            answer = input("Do you want to restart the game [y or n] ?: ")
        if answer == "y":
            main()
        else:
            print("Thank you for playing ! ")
            exit()
    
  3. This change is optional and is purely my opinion to make the output look better.

    def printout_cards():
        print("######################################\n")
        print("Player card: {}".format(" ".join(player_card)))
        print("\n######################################\n")
        print("Computer card: {}".format(" ".join(computer_card)))
    
  4. I would suggest not naming a function random_choice() for readability. It can easily be confused with random.choice().

\$\endgroup\$
1
  • \$\begingroup\$ thnaks alot for the review and interesting tips m8 ! \$\endgroup\$ Jul 14, 2017 at 3:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.