Created
December 18, 2025 09:22
-
-
Save alhatimym/74fd2f84d6e3716a492d27976236a200 to your computer and use it in GitHub Desktop.
I made a Twnty one game, thanks to mr.Ibrahim Adel for teaching me how to do it go look at his course, it is realy good, i learned all of those things with him.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import random | |
| def deal_card(cards): | |
| return random.choice(cards) | |
| def adjust_for_ace(hand): | |
| if sum(hand) > 21 and 11 in hand: | |
| hand[hand.index(11)] = 1 | |
| def game(): | |
| print("Welcome to the Twenty-One Game!") | |
| cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10] | |
| user_cards = [deal_card(cards), deal_card(cards)] | |
| computer_cards = [deal_card(cards), deal_card(cards)] | |
| game_over = False | |
| while not game_over: | |
| adjust_for_ace(user_cards) | |
| adjust_for_ace(computer_cards) | |
| print(f"Your cards: {' '.join(map(str, user_cards))} (Total: {sum(user_cards)})") | |
| print(f"Computer's first card: {computer_cards[0]}") | |
| if sum(user_cards) == 21: | |
| print("You won! It's a BLACKJACK!") | |
| return | |
| if sum(computer_cards) == 21: | |
| print("Computer won! It's a BLACKJACK!") | |
| return | |
| choice = input("Do you want to take another card? (y/n)\n--> ").lower() | |
| if choice == "y": | |
| user_cards.append(deal_card(cards)) | |
| if sum(user_cards) > 21: | |
| adjust_for_ace(user_cards) | |
| if sum(user_cards) > 21: | |
| print("You went over 21. You lose.") | |
| return | |
| else: | |
| game_over = True | |
| while sum(computer_cards) < 17: | |
| computer_cards.append(deal_card(cards)) | |
| adjust_for_ace(computer_cards) | |
| print(f"Your final hand: {user_cards} (Total: {sum(user_cards)})") | |
| print(f"Computer's final hand: {computer_cards} (Total: {sum(computer_cards)})") | |
| if sum(computer_cards) > 21: | |
| print("Computer went over 21. You win!") | |
| elif sum(user_cards) > sum(computer_cards): | |
| print("You win!") | |
| elif sum(user_cards) < sum(computer_cards): | |
| print("You lose.") | |
| else: | |
| print("It's a draw.") | |
| game() |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Woow what a project!
I leaned allot form it