I just tweaked my previous code and ended up with:
import randomimport artcards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]while True:first_inquiry = input("Do you want to play a game of Blackjack? Type 'y' or 'n': ")if first_inquiry == "y":print("\n*20")print(art.logo)your_score = 0your_cards = random.sample(cards, k=2)for card in your_cards:your_score += cardprint(f"your_cards: {your_cards}, current score: {your_score}")computer_score = 0computer_cards = random.sample(cards, k=2)print(f"Computer's first card: {computer_cards[0]}")for card in computer_cards:computer_score += cardsecond_inquiry = input("Type 'y' to get another card, type 'n' to pass: ")if second_inquiry == "n":print(f"Your final hand: {your_cards}, final score: {your_score}")if computer_score > your_score:print(f"Computer's final hand: {computer_cards}, final score: {computer_score}")print("You lose 😤")elif computer_score == your_score:print(f"Computer's final hand: {computer_cards}, final score: {computer_score}")print("Push")elif computer_score <= 17:computer_cards.append(random.choice(cards))computer_score += computer_cards[2]if computer_score > 21:print(f"Computer's final hand: {computer_cards}, final score: {computer_score}")print("Opponent went over. You win 😁")else:print(f"Computer's final hand: {computer_cards}, final score: {computer_score}")print("You win 😁")else:your_cards.append(random.choice(cards))your_score += your_cards[2]print(f"Your final hand: {your_cards}, final score: {your_score}")if your_score > 21:print(f"Computer's final hand: {computer_cards}, final score: {computer_score}")print("You went over. You lose 😤")elif computer_score <= 17:computer_cards.append(random.choice(cards))computer_score += computer_cards[2]if computer_score > 21:print(f"Computer's final hand: {computer_cards}, final score: {computer_score}")print("Opponent went over. You win 😁")elif computer_score > your_score:print(f"Computer's final hand: {computer_cards}, final score: {computer_score}")print("You lose 😤")else:print(f"Computer's final hand: {computer_cards}, final score: {computer_score}")print("Push")elif first_inquiry == "n":breakelse:print("Invalid key. Type 'y' or 'n': ")
for the Blackjack game. I should have saved the previous code somewhere, because how do I compare them now?
Better still, I should have made use of git or not be too confident that I couldn't destroy the code.
Although the previous code was shorter and worked almost perfectly, it tended to ignore the "elif computer_score <= 17:.." line.
This particular code, restarts when computer_score < your_score.
I don't have a copy, I can't compare them. So, good night.
Comments
Post a Comment