import random
def roll_dice():
"""Roll two dice and return their values and sum."""
die1 = random.randint(1, 6)
die2 = random.randint(1, 6)
total = die1 + die2
print(f"You rolled {die1} + {die2} = {total}")
return total
def play_dice_game():
"""
Play one round of the dice game.
Returns True if player wins, False if player loses.
"""
first_roll = roll_dice()
if first_roll in (7, 11):
print("You win!")
return True
elif first_roll in (2, 3, 12):
print("Craps! You lose!")
return False
else:
point = first_roll
print(f"Point is set to {point}. Keep rolling until you roll {point} (win) or 7 (lose).")
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 function with game loop and statistics."""
wins = 0
losses = 0
while True:
choice = input("Do you want to play a round? (y/n): ").strip().lower()
if choice == 'y':
result = play_dice_game()
if result:
wins += 1
else:
losses += 1
print(f"Current Stats: Wins: {wins}, Losses: {losses}\n")
elif choice == 'n':
print("\nFinal Stats:")
print(f"Wins: {wins}, Losses: {losses}")
print("Thanks for playing!")
break
else:
print("Invalid input. Please enter 'y' or 'n'.")
if __name__ == "__main__":
print("Welcome to the Dice Game!")
main()
Popcorn hack 1
-
Cryptography:
Random numbers are critical for generating secure encryption keys, passwords, and tokens. If the numbers aren’t truly random, attackers could predict them and compromise sensitive data. High-quality randomness ensures secure communication and data protection. -
Simulations (e.g., Weather Forecasting or Stock Market Modeling):
Random numbers are used to model uncertainty and variability in complex systems. For example, weather models use randomness to simulate atmospheric changes, and financial models use it to predict market fluctuations and risk scenarios.
Popcorn hack 2
import random
def magic_8_ball():
"""
Simulates a Magic 8-Ball response:
- Returns "Yes" ~50% of the time
- Returns "No" ~25% of the time
- Returns "Ask again later" ~25% of the time
"""
roll = random.randint(1, 4) # Generates a number between 1 and 4
if roll in (1, 2):
return "Yes"
elif roll == 3:
return "No"
else:
return "Ask again later"
# Example usage
for _ in range(10):
print(magic_8_ball())
popcorn hack 3
def traffic_light_simulation():
cycle = ["Green"] * 5 + ["Yellow"] * 2 + ["Red"] * 4 # Full cycle = 11 steps
for step in range(20):
light = cycle[step % len(cycle)] # Loop through the cycle
print(f"Time step {step + 1}: {light}")
traffic_light_simulation()
This is a simulation because it models the behavior of a traffic light system over time without using a real traffic light. Its real-world impact includes helping engineers test timing sequences and improve traffic flow and safety before actual implementation in intersections.