I have a simple task of creating a simple lotto game to practice python. I had some troubles with return of functions so i dropped few because of not understanding the scopes maybe.
In my next version i want to try to make it fully in functions, and in the next version i want to do it in OOP principles.
By posting this code I would like to hear maybe some suggestions where my logic was wrong and some PEP comments.
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 number_pool = list(range(1, 91)) computer_card = random.sample(number_pool, 15) computer_card_sorted = sorted(computer_card) player_card = random.sample(number_pool, 15) player_card_sorted = sorted(player_card) def display_cards(): print("Computer card:\n") print(computer_card_sorted) print("====================================") print("Player card:\n") print(player_card_sorted) def lotto_choice(): choice = random.choice(number_pool) number_pool.remove(choice) return choice def the_game(): while number_pool: choice = lotto_choice() print("The random lotto is: " + str(choice)) display_cards() cross_number = input("Do you want to cross out a number") cross_number.lower() if cross_number == "y": if choice in player_card_sorted: player_card_sorted.remove(choice) elif choice in computer_card_sorted: computer_card_sorted.remove(choice) if cross_number == "n": if choice in computer_card_sorted: computer_card_sorted.remove(choice) else: continue else: if len(player_card_sorted) == 0: print("Congratulations Player ! You won") elif len(computer_card_sorted) == 0: print("The computer have won, too bad !") else: print("It is a tie you both ran out of numbers, very straange !") the_game()