如何在Python中让两个自定义函数重复执行5次并实现指定游戏规则
Let's tackle this step by step—first adding the critical 5-round loop, then working through all your game success criteria to make the complete, functional game you want.
Key Changes & Feature Implementation
First, we'll clean up the code structure (ditching messy global variables) and then build out every required feature:
1. 5-Round Loop & Score Accumulation
We'll use a for loop to run exactly 5 rounds, tracking each player's total score as we go instead of relying on global variables.
2. Double Dice Bonus Roll
When a player rolls doubles (both dice match), we'll add an extra dice roll and tack that value onto their round score.
3. Tiebreaker Mechanism
If scores are tied after 5 rounds, we'll have each player roll a single die repeatedly until one gets a higher roll.
4. Non-Negative Scores
We'll make sure both round scores and total scores never drop below 0, per your requirement.
Updated Complete Code
import random import time def play_round(player_name): print(f"\n{player_name} is up!") roll = input("Please enter ROLL to roll the dice > ").strip().upper() if roll != "ROLL": print("Exiting game...") exit() # Roll two 6-sided dice num1 = random.randint(1, 6) num2 = random.randint(1, 6) print(f"You rolled a {num1} and a {num2}") round_score = num1 + num2 # Handle doubles bonus roll if num1 == num2: extra_roll = random.randint(1, 6) print(f"Doubles! Extra roll: {extra_roll}") round_score += extra_roll # Adjust score for even/odd total if round_score % 2 == 0: round_score += 10 else: round_score -= 5 # Ensure score never goes below 0 round_score = max(round_score, 0) print(f"Your round score: {round_score}") return round_score def tiebreaker(): print("\nIt's a tie! Starting tiebreaker rounds...") while True: time.sleep(1) # Player One's tiebreaker roll p1_roll = random.randint(1, 6) print(f"\nPlayer One rolls: {p1_roll}") # Player Two's tiebreaker roll p2_roll = random.randint(1, 6) print(f"Player Two rolls: {p2_roll}") if p1_roll > p2_roll: return "Player One" elif p2_roll > p1_roll: return "Player Two" else: print("Still tied! Rolling again...") # Initialize total scores player1_total = 0 player2_total = 0 # Run 5 full rounds for round_num in range(1, 6): print(f"\n=== Round {round_num} ===") player1_total += play_round("Player One") time.sleep(2) player2_total += play_round("Player Two") time.sleep(1) print(f"\nCurrent Scores:\nPlayer One: {player1_total} | Player Two: {player2_total}") # Final score check and winner announcement print("\n=== Final Scores ===") print(f"Player One: {player1_total}") print(f"Player Two: {player2_total}") if player1_total > player2_total: print("Player One has won the game!") elif player2_total > player1_total: print("Player Two has won the game!") else: winner = tiebreaker() print(f"{winner} wins the tiebreaker!")
How This Meets All Your Success Criteria
Let's map each requirement directly to the code:
- 1. Two 6-sided dice per player: Handled in
play_round()withnum1 = random.randint(1,6)andnum2 = random.randint(1,6) - 2. Round score calculation & output: The
play_round()function returns each round's score, which we add to the total, and we print the round score after every turn - 3. 5 rounds of play: The
for round_num in range(1,6)loop runs exactly 5 times - 4. Tiebreaker for tied scores: The
tiebreaker()function runs a loop of single-dice rolls until one player has a higher result - 5. Post-5-round winner output: We compare total scores and print the winner, or trigger the tiebreaker if needed
- 6. Bonus roll for doubles: In
play_round(), we check ifnum1 == num2, then roll an extra die and add its value to the round score - 7. No negative scores: We use
round_score = max(round_score, 0)to ensure round scores never go negative, and since we only add positive values to totals, totals stay non-negative too
Quick Code Improvements Note
- Combined player one and two logic into a single
play_round()function (since their turns are identical) to cut down on duplicate code - Ditched global variables in favor of return values and total score variables—this makes the code cleaner and easier to debug
- Added round labels and current score updates after each round for better user experience
- Small
time.sleep()delays make the game flow feel more natural instead of jumping straight from one turn to the next
内容的提问来源于stack exchange,提问作者17hturner

