Skip to main content

365 DAYS OF CODE - DAY 15

I just tweaked my previous code and ended up with:

import random
import art

cards = [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 = 0
your_cards = random.sample(cards, k=2)

for card in your_cards:
your_score += card
print(f"your_cards: {your_cards}, current score: {your_score}")

computer_score = 0
computer_cards = random.sample(cards, k=2)
print(f"Computer's first card: {computer_cards[0]}")

for card in computer_cards:
computer_score += card

second_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":
break

else:
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

Popular posts from this blog

365 DAYS OF CODE - DAY 0

DAY 0 THOUGHT PROCESS Day Zero of the 365 days of code highlights my state of mind before the actual start of the program. Yes, it is a program. A program I set for myself to make sure I actually put in the work on a daily. I have tried coding before but have never been quite consistent with it. A friend advised me to try coding daily if I really wanted to be great at it someday. He also stated that it would take a long time and I should be prepared for it. During the same period, I noticed he was on a year's worth streak of chess which made me realize I could also do the same but with coding. I signed up for the 100 days of code course on Udemy and plan on starting my coding journey with Angela Yu and the thousands of students also taking the course. I also drafted my own copy of the pledge, copied and modified from the 100 Days of Python Pledge from the course materials. It is currently pasted on my wall as a constant reminder. Finally, I will also be recording my process here...

365 DAYS OF CODE - DAY 13

What is wrong with this code?? Can you tell what it does?? import art n1 = 0 n2 = 0 selected_operation = "" solution = 0 def add (n1, n2): return n1 + n2 def subtract (n1, n2): return n1 - n2 def multiply (n1, n2): return n1 * n2 def divide (n1, n2): return n1 / n2 operations = { "+" : add, "-" : subtract, "*" : multiply, "/" : divide } def rest_of_calculation (): print ( "+ \n - \n * \n /" ) selected_operation = input ( "Pick an operation: " ) n2 = float ( input ( "What is the next number?: " )) for key in operations: if key == selected_operation: solution = operations[key](n1, n2) print ( f" { n1 } { key } { n2 } = { solution } " ) print (art.logo) n1 = float ( input ( "What is the first number?: " )) rest_of_calculation() while True : new_calculation = input ( f"Type 'y' to continue calcu...

365 DAYS OF CODE - DAY 10

I realized that I have been tagging each day in accordance with the 100 days of python course . Now, I am one day behind (it is day 10 of the 365 days of code but day 9 on the python course I am taking right now) because I made sure to review unclear exercises yesterday. It is fine regardless. On day 9 of the python course, we looked at dictionaries, its table-likeness (2 columned) One tiny forgotten comma leads to 4 different errors and 1 warning, the prominent one being the SyntaxError. But shouldn’t it highlight line 3 instead of line 4?? Comma added, Error fixed. My Grading Program Code: student_scores = {     'Harry': 88,     'Ron': 78,     'Hermione': 95,     'Draco': 75,     'Neville': 60 }   student_grades = {}   for key in student_scores:     if student_scores[key] > 90:         student_scores[key] = "O...