Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python simple programms #256

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions python/blackjack_game.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import random

# Define card values
card_values = {
'Ace': 11,
'2': 2,
'3': 3,
'4': 4,
'5': 5,
'6': 6,
'7': 7,
'8': 8,
'9': 9,
'10': 10,
'Jack': 10,
'Queen': 10,
'King': 10
}

# Define deck of cards
deck = list(card_values.keys()) * 4

# Define function to calculate hand value
def calculate_hand(hand):
value = sum(card_values[card] for card in hand)
if value > 21 and 'Ace' in hand:
value -= 10
return value

# Shuffle deck
random.shuffle(deck)

# Deal initial hands
player_hand = [deck.pop(), deck.pop()]
dealer_hand = [deck.pop(), deck.pop()]

# Display initial hands
print("Player's hand:", player_hand)
print("Dealer's hand:", [dealer_hand[0], '???'])

# Player's turn
while True:
choice = input("Hit or stand? ")
if choice.lower() == 'hit':
player_hand.append(deck.pop())
print("Player's hand:", player_hand)
if calculate_hand(player_hand) > 21:
print("Bust! Dealer wins.")
break
elif choice.lower() == 'stand':
break

# Dealer's turn
if calculate_hand(player_hand) <= 21:
print("Dealer's hand:", dealer_hand)
while calculate_hand(dealer_hand) < 17:
dealer_hand.append(deck.pop())
print("Dealer's hand:", dealer_hand)
if calculate_hand(dealer_hand) > 21:
print("Dealer busts! Player wins.")
break

# Determine winner
if calculate_hand(player_hand) <= 21 and calculate_hand(dealer_hand) <= 21:
if calculate_hand(player_hand) > calculate_hand(dealer_hand):
print("Player wins!")
elif calculate_hand(player_hand) < calculate_hand(dealer_hand):
print("Dealer wins!")
else:
print("It's a tie!")
16 changes: 16 additions & 0 deletions python/calculator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Get user input
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
op = input("Enter operation (+, -, *, /): ")

# Perform operation and display result
if op == "+":
print(num1 + num2)
elif op == "-":
print(num1 - num2)
elif op == "*":
print(num1 * num2)
elif op == "/":
print(num1 / num2)
else:
print("Invalid operation")
7 changes: 7 additions & 0 deletions python/random_number.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import random

# Generate a random integer between 1 and 10
num = random.randint(1, 10)

# Display the random number
print("Random number:", num)