Popcorn Hacks
Popcorn Hack #1
import random
def roll_dice():
# Simulate a 6-sided dice roll (returns a number between 1 and 6)
return random.randint(1, 6)
# Example usage:
print("Dice roll:", roll_dice())
Dice roll: 2
Popcorn Hack #2
import random
def biased_color():
colors = ["Red", "Blue", "Green", "Yellow", "Purple", "Orange"]
weights = [0.50, 0.30, 0.04, 0.04, 0.04, 0.04] # Total = 1.00
# Print 10 biased random colors
for _ in range(10):
print(random.choices(colors, weights=weights, k=1)[0])
# Call the function
biased_color()
Red
Blue
Red
Blue
Blue
Yellow
Green
Blue
Red
Red
Homework Hacks
Homework Hack 1
import random
def coin_flip_game():
player1_heads = 0
player2_heads = 0
rounds = 0
while player1_heads < 3 and player2_heads < 3:
rounds += 1
flip1 = random.choice(["heads", "tails"])
flip2 = random.choice(["heads", "tails"])
if flip1 == "heads":
player1_heads += 1
if flip2 == "heads":
player2_heads += 1
print(f"Round {rounds}: Player 1 - {flip1}, Player 2 - {flip2}")
print(f"Score: Player 1 = {player1_heads}, Player 2 = {player2_heads}\n")
if player1_heads == 3:
print(f"Player 1 wins in {rounds} rounds!")
else:
print(f"Player 2 wins in {rounds} rounds!")
# Run the game
coin_flip_game()
Round 1: Player 1 - heads, Player 2 - tails
Score: Player 1 = 1, Player 2 = 0
Round 2: Player 1 - heads, Player 2 - heads
Score: Player 1 = 2, Player 2 = 1
Round 3: Player 1 - tails, Player 2 - tails
Score: Player 1 = 2, Player 2 = 1
Round 4: Player 1 - heads, Player 2 - tails
Score: Player 1 = 3, Player 2 = 1
Player 1 wins in 4 rounds!