#actual hw
import random
def roll_dice():
"""Roll two dice and return their values and sum."""
die1 = random.randint(1, 6)
die2 = random.randint(1, 6)
dice_sum = die1 + die2
print(f"You rolled {die1} + {die2} = {dice_sum}")
return die1, die2, dice_sum
def play_dice_game():
"""
Play one round of the dice game.
Returns True if the player wins, False if the player loses.
"""
# First roll
_, _, dice_sum = roll_dice()
# Win on first roll if sum is 7 or 11.
if dice_sum in (7, 11):
print("You win!")
return True
# Lose on first roll if sum is 2, 3, or 12.
elif dice_sum in (2, 3, 12):
print("You lose!")
return False
else:
# Otherwise, the dice sum becomes the point.
point = dice_sum
print(f"Your point is {point}. Keep rolling!")
# Continue rolling until the player wins or loses.
while True:
_, _, dice_sum = roll_dice()
if dice_sum == point:
print("You hit your point. You win!")
return True
elif dice_sum == 7:
print("You rolled a 7 before hitting your point. You lose!")
return False
def main():
"""Main game function with game loop and statistics."""
wins = 0
losses = 0
while True:
play_choice = input("Do you want to play? (y/n): ").strip().lower()
if play_choice == 'y':
if play_dice_game():
wins += 1
else:
losses += 1
print(f"Current Statistics: Wins: {wins}, Losses: {losses}\n")
elif play_choice == 'n':
print(f"\nFinal Statistics:\nWins: {wins}\nLosses: {losses}")
print("Thanks for playing!")
break
else:
print("Invalid input, please type 'y' or 'n'.")
if __name__ == "__main__":
print("Welcome to the Dice Game!")
main()
Welcome to the Dice Game!
You rolled 1 + 6 = 7
You win!
Current Statistics: Wins: 1, Losses: 0
You rolled 3 + 2 = 5
Your point is 5. Keep rolling!
You rolled 3 + 6 = 9
You rolled 3 + 1 = 4
You rolled 5 + 2 = 7
You rolled a 7 before hitting your point. You lose!
Current Statistics: Wins: 1, Losses: 1
You rolled 6 + 2 = 8
Your point is 8. Keep rolling!
You rolled 1 + 6 = 7
You rolled a 7 before hitting your point. You lose!
Current Statistics: Wins: 1, Losses: 2
You rolled 5 + 2 = 7
You win!
Current Statistics: Wins: 2, Losses: 2
Invalid input, please type 'y' or 'n'.
Invalid input, please type 'y' or 'n'.
Final Statistics:
Wins: 2
Losses: 2
Thanks for playing!