Golf Score Calculator Project

GOLF SCORE CALCULATOR PROJECT

Python Dictionaries Practice

The golf scorecards of 4 golf players are stored in 4 dictionaries, each one corresponding to a player. The key of the dictionaries is the hole number and the value corresponds to the strokes.

Dictionary “Holes” is the holes list and it contains the number of strokes it takes to finish each hole of the golf course. The dictionaries “Player1”, “Player2”, “Player3” and “Player4” contains the number of strokes each player has made in each hole.

Holes = {1:3,2:4,3:4,4:4,5:5,6:4,7:3,8:4,9:5}

Player1 = {1:4, 2:4, 3:4, 4:3, 5:5, 6:5, 7:4, 8:5,5:6}
Player 2= {1:5, 2:6, 3:6, 4:4, 5:5, 6:7, 7:3, 8:5, 9:7}
Player3 = {1:3, 2:4, 3:3, 4:4, 5:4, 6:4, 7:2, 8:4, 9:4}
Player 4 = {1:4, 2:5, 3:5, 4:6, 5:4, 6:6, 7:3, 8:6, 9:6}

So for example, according to the “Holes” dictionary, hole 1 is a par 3 and therefore it must be finished in 3 strokes. Given that only Player 3 has made three strokes, he is the winner of hole 1. Hence, the winner of the match is the player who finishes the 9 holes course with the minimum number of strokes. See here for information about Golf rules.

Your task is to write a script that displays the winner of the 9 holes match along with the number of strokes.

The purpose of this Python dictionaries practice is to make you work with dictionaries and loops so you fully understand how they work and the multiple applications for real-life projects.

Expected Ouput

The winner of the golf tournament is Player3 with a total score of 32

Steps

  1. Create the Dictionaries:
    • Define the "Holes" dictionary that contains the number of strokes it takes to finish each hole of the golf course.
    • Define dictionaries for each player (Player1, Player2, Player3, and Player4) containing the number of strokes each player has made in each hole.
  2. Calculate Scores:
    • Calculate the total score for each player by adding up the strokes for all 9 holes.
  3. Determine the Winner:
    • Compare the total scores of all players to find the player with the minimum total score.
  4. Display the Winner:
    • Print the player with the minimum total score as the winner of the golf tournament.

SOURCE CODE

## We define the dictionaries
Holes = {1:5, 2:4, 3:4, 4:4, 5:5, 6:4, 7:3, 8:4, 9:5}
Player1 = {1:4, 2:4, 3:4, 4:3, 5:5, 6:5, 7:4, 8:5, 9:6}
Player2 = {1:5, 2:6, 3:6, 4:4, 5:5, 6:7, 7:3, 8:5, 9:7}
Player3 = {1:3, 2:4, 3:3, 4:4, 5:4, 6:4, 7:2, 8:4, 9:4}
Player4 = {1:4, 2:5, 3:5, 4:6, 5:4, 6:6, 7:3, 8:6, 9:6}

players = {"Player1": Player1, "Player2": Player2, "Player3": Player3, "Player4": Player4}

# Calculate scores
scores = {}
for player, scorecard in players.items():
    total_score = sum(scorecard[hole] for hole in scorecard)
    scores[player] = total_score

# Determine the winner
winner = min(scores, key=scores.get)

# Display the winner
print(f"The winner of the golf tournament is {winner} with a total score of {scores[winner]}.")

See here for mre fun and real Python projects!

Practity
We will be happy to hear your thoughts

Leave a reply

Python and Excel Projects for Practice
Register New Account
Shopping cart