Blackjack game with Python
Create art.py file and paste code below
logo = """
.------. _ _ _ _ _
|A_ _ |. | | | | | | (_) | |
|( \/ ).-----. | |__ | | __ _ ___| | ___ __ _ ___| | __
| \ /|K /\ | | '_ \| |/ _` |/ __| |/ / |/ _` |/ __| |/ /
| \/ | / \ | | |_) | | (_| | (__| <| | (_| | (__| <
`-----| \ / | |_.__/|_|\__,_|\___|_|\_\ |\__,_|\___|_|\_\\
| \/ K| _/ |
`------' |__/
"""
Inside main.py file, paste this code
from art import logo
print(logo)
import random
cards = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
first_card_player = random.choice(cards)
second_card_player = random.choice(cards)
player_cards = [first_card_player, second_card_player]
first_card_computer = random.choice(cards)
second_card_computer = random.choice(cards)
computer_cards = [first_card_computer, second_card_computer]
print(f"Computer Cards: {computer_cards}")
print(f"Your cards: {player_cards}")
computer_score = first_card_computer + second_card_computer
player_score = first_card_player + second_card_player
def score_adder(computer_score, player_score):
if computer_score > 21 or player_score == 21:
return "You win"
if player_score > 21 or computer_score == 21:
return "You loose"
winner = score_adder(computer_score, player_score)
game_continue = False
while not game_continue:
if player_score < 21:
want_continue = input("Do you want next card? Y/N: \n").lower()
if want_continue == "y":
next_choice = random.choice(cards)
player_cards.append(next_choice)
print(player_cards)
player_score = player_score + next_choice
winner = score_adder(computer_score, player_score)
else:
print("Now its computer's turn")
while computer_score < 21 and computer_score < player_score:
computer_score = computer_score + random.choice(cards)
winner = score_adder(computer_score, player_score)
print(player_cards)
print(computer_cards)
game_continue = True
print(winner)
Comments
Post a Comment