Popcorn Hack 1
2 real world examples of using a random number generator are rolling dice with equal probability for all the numbers on the die, especially in gambling situations, as well as randomizing numbers and letters for secure recommended passwords to ensure no breaches occur
Popcorn Hack 2
import random
def magic_8_ball():
num = random.randint(1, 4) # Generates a number from 1 to 4
if num in [1, 2]:
return "Yes"
elif num == 3:
return "No"
else:
return "Ask again later"
# Test your function
for i in range(15):
print(f"Magic 8-Ball says: {magic_8_ball()}")
Magic 8-Ball says: Yes
Magic 8-Ball says: No
Magic 8-Ball says: Yes
Magic 8-Ball says: Ask again later
Magic 8-Ball says: Yes
Magic 8-Ball says: No
Magic 8-Ball says: Ask again later
Magic 8-Ball says: Yes
Magic 8-Ball says: Yes
Magic 8-Ball says: Ask again later
Magic 8-Ball says: Yes
Magic 8-Ball says: Yes
Magic 8-Ball says: Yes
Magic 8-Ball says: Yes
Magic 8-Ball says: Yes
Popcorn Hack 3
This is a simulation because it simulates when traffic lights turn to each color based on a specified pattern in the Python code. This kind of traffic light simulation will have real world impact because drivers will know what to expect when nearing a traffic light(how much time between each light color change), reducing congestion and traffic as a whole.
states = ["Green", "Yellow", "Red"]
durations = {"Green": 5, "Yellow": 2, "Red": 4}
timeline = []
# Simulate 20 time steps
time = 0
state = "Green"
counter = 0
while time < 20:
timeline.append((time, state))
counter += 1
if counter == durations[state]:
counter = 0
current_index = states.index(state)
state = states[(current_index + 1) % len(states)]
time += 1
for t, s in timeline:
print(f"Time {t}: {s}")
Time 0: Green
Time 1: Green
Time 2: Green
Time 3: Green
Time 4: Green
Time 5: Yellow
Time 6: Yellow
Time 7: Red
Time 8: Red
Time 9: Red
Time 10: Red
Time 11: Green
Time 12: Green
Time 13: Green
Time 14: Green
Time 15: Green
Time 16: Yellow
Time 17: Yellow
Time 18: Red
Time 19: Red
Homework Hack 1
import random
def roll_dice():
"""🎲 Roll two dice and return their individual values and the total sum."""
die1 = random.randint(1, 6)
die2 = random.randint(1, 6)
total = die1 + die2
print(f"Rolled: {die1} + {die2} = {total}")
return total
def play_dice_game():
"""
▶️ Play one round of the dice game.
Rules:
- Win on first roll if sum is 7 or 11
- Lose on first roll if sum is 2, 3, or 12
- Any other number becomes the 'point'
- Keep rolling until you roll the point again (win) or a 7 (lose)
Returns True if player wins, False if player loses.
"""
first_roll = roll_dice()
# Immediate win condition
if first_roll in (7, 11):
print("You win!")
return True
# Immediate loss condition
if first_roll in (2, 3, 12):
print("You lose!")
return False
# Set point and enter roll loop
point = first_roll
print(f"Your point is {point}. Keep rolling!")
while True:
roll = roll_dice()
if roll == point:
print("You rolled the point again! You win!")
return True
elif roll == 7:
print("You rolled a 7. You lose!")
return False
def main():
"""📊 Main game loop with player prompt and win/loss tracking."""
wins = 0
losses = 0
print("🎲 Welcome to the Dice Game!")
while True:
choice = input("\nDo you want to play a round? (yes/no): ").strip().lower()
if choice in ("yes", "y"):
result = play_dice_game()
if result:
wins += 1
else:
losses += 1
print(f"Stats ➤ Wins: {wins}, Losses: {losses}")
elif choice in ("no", "n"):
print("\nThanks for playing!")
print(f"Final Stats ➤ Wins: {wins}, Losses: {losses}")
break
else:
print("Invalid input. Please type 'yes' or 'no'.")
# Run the game
if __name__ == "__main__":
main()
🎲 Welcome to the Dice Game!
Rolled: 1 + 2 = 3
You lose!
Stats ➤ Wins: 0, Losses: 1
Rolled: 3 + 4 = 7
You win!
Stats ➤ Wins: 1, Losses: 1
Rolled: 2 + 4 = 6
Your point is 6. Keep rolling!
Rolled: 6 + 6 = 12
Rolled: 3 + 1 = 4
Rolled: 5 + 1 = 6
You rolled the point again! You win!
Stats ➤ Wins: 2, Losses: 1
Rolled: 3 + 6 = 9
Your point is 9. Keep rolling!
Rolled: 1 + 3 = 4
Rolled: 1 + 2 = 3
Rolled: 2 + 4 = 6
Rolled: 6 + 1 = 7
You rolled a 7. You lose!
Stats ➤ Wins: 2, Losses: 2
Thanks for playing!
Final Stats ➤ Wins: 2, Losses: 2