If you are looking for your next python project, the golf scores program is one of the most popular and easy-to-code python programs out there. If you are a python beginner, coding this program will enhance your familiarity with the programming language as well as with the decision making “if-else” statements and control flow statements such as the for loop.
In the golf scores program, besides the “if-else” branching, we will also learn how to read from and write text to files in python.
This article guides you through creating a Golf Scores Program in Python. The program helps beginners enhance their familiarity with Python’s syntax, logic, and control flow. It covers basic golf terminologies, program flow, and functions for creating new records, searching for players, and displaying records.
Basic Structure of the Golf Scores Program
The golf score program will contain the following:
A TEXT(.txt) FILE, which will contain the records of the name of players, their scores(number of strokes), pars, etc.
A MAIN MENU, which will be the basic interface for the user. It will be used to cater to the different needs of our users such as, record maintenance or to display a particular players stats.
Related: Decreasing For Loops in Python.
Basic Golf Terminologies for This Program
Some scoring conventions in golf are:
- STROKE: The number of forward club swings that are intended for hitting the golf ball.
- PAR: The ideal number of strokes required by an expert player for making a given hole.
- CONDOR: Four strokes under par.
- ALBATROSS/DOUBLE EAGLE: Three strokes under par.
- EAGLE: Two strokes under par.
- BIRDIE: One stroke under par.
- ACE: Getting the ball in the cup in only one stroke.
- BOGEY: One stroke over par.
- DOUBLE BOGEY: Two strokes over par.
- TRIPLE BOGEY; Three strokes over par.
- QUADRUPLE BOGEY: Four strokes over par.
Now that we have a basic understanding of golf terminology, let’s discuss the flow of our program.
Program Flow
The user would be prompted with a menu that will make him/her aware of the features of our program. There would be four main options:
- To enter a new record in the database, that is essentially our text file.
- To search for a player.
- To display all the records from the text file.
- To quit.

Depending upon the user’s choice whether he selects 1,2,3 or 4, respective programs will be called to carry out each task.
We already have a file called “scoresforgolf.txt” with 3 existing records in the form of: NAME|SCORE|PAR|SCORING CONVENTION. You can create your own text files however you want.

Creating the various functions
Now, let’s create functions to perform different activities depending upon the user’s choice. We will start with the first option in the previous section, that is, To create a new record consisting of a player’s name, scores, par and calculating their scoring conventions depending upon their scores and the given pars. Conventionally, the par can only take up three values, 3,4 and 5. In some cases it can be more than that but in this program we will limit the par to these three values only.
The function is named createrecord() and we can implement it in the following way. The new name of the player, the scores and the par of the match are all going to be inputs from the user.
#creating new record
def createrecord():
noofrec=int(input("How many records do you want to add? "))
f3=open("/content/scoresforgolf.txt",'a')
for i in range(noofrec):
L=''
name=input("enter the name of player= ")
score=int(input("enter score of player="))
par=int(input("enter par (NOTE: PAR CAN ONLY TAKE ON THE FOLLOWING VALUES=3,4 OR 5)= "))
if (par==3 or par==4 or par==5) and (score!=1):
if(score-par==-4):
convention="Condor"
elif(score-par==-3):
convention="Albatross(Double Eagle)"
elif(score-par==-2):
convention="Eagle"
elif(score-par==-1):
convention="Birdie"
elif(score-par==0):
convention="At par"
elif(score-par==1):
convention="Bogey"
elif(score-par==2):
convention="Double bogey"
elif(score-par==3):
convention="Triple bogey"
elif(score-par==4):
convention="Quadruple bogey"
else:
convention="-"
elif(par==3 or par==4 or par==5) and (score==1):
convention="Ace!"
else:
print("wrong output")
quit()
f3.write(name+'|'+str(score)+'|'+str(par)+'|'+ convention+'\n')
print("The new record/records have been created.")
f3.close()
Now, we will create another function which will search for a given player when prompted with the name.
#displaying a particular record using an user defined function
def displayy():
name=input("Enter the name of player:")
f2=open("/content/scoresforgolf.txt",'r')
for i in f2:
L=[]
L=i.split("|")
if (L[0]==name):
print("The required statistics of the player is= ")
print(i)
break
f2.close()
Now, the third function will display all the records in our database.
#function for displaying all the records
def displayall():
f1=open("/content/scoresforgolf.txt",'r')
print("The records are in the following format:")
print("NAME"+"|SCORE"+"|PAR"+"|SCORING CONVENTION")
for i in f1:
print(i)
f1.close()
Now, let’s look at the driver code which will display the options to our users so that they can make an informed decision.
#---------beginning of the golf score program---------------
print("="*60)
print("WELCOME TO THE GOLF SCORES PROGRAM!")
print("="*60)
#driver code
inp=int(input("SELECT YOUR REQUIRED OPTION:"+'\n'+"1: To enter new record"+'\n'+
"2: To search for a player"+'\n'+'3: To display all records'+'\n'+
'4: To quit'+'\n'))
if (inp==1):
createrecord()
elif(inp==2):
displayy()
elif(inp==3):
displayall()
elif(inp==4):
quit()
else:
print("wrong input!")
Outputs from the options in the menu
Now, let’s say a person wants to create a new record in our text file. He/she chooses the first option, and this is how it will look:
============================================================
WELCOME TO THE GOLF SCORES PROGRAM!
============================================================
SELECT YOUR REQUIRED OPTION:
1: To enter new record
2: To search for a player
3: To display all records
4: To quit
1
How many records do you want to add? 1
enter the name of player= Charlie
enter score of player=6
enter par (NOTE: PAR CAN ONLY TAKE ON THE FOLLOWING VALUES=3,4 OR 5)= 3
The new record/records have been created.
Next, let’s say we need to find a player named “Eva” from our database. Let’s see what output we will get if we select option 2.
============================================================
WELCOME TO THE GOLF SCORES PROGRAM!
============================================================
SELECT YOUR REQUIRED OPTION:
1: To enter new record
2: To search for a player
3: To display all records
4: To quit
2
Enter the name of player:Eva
The required statistics of the player is=
Eva|1|3|Ace!
Finally, let’s display all the records in our text file “scoresforgolf.txt”.
============================================================
WELCOME TO THE GOLF SCORES PROGRAM!
============================================================
SELECT YOUR REQUIRED OPTION:
1: To enter new record
2: To search for a player
3: To display all records
4: To quit
3
The records are in the following format:
NAME|SCORE|PAR|SCORING CONVENTION
Mark|5|4|Bogey
Celine|9|5|Quadruple bogey
Eva|1|3|Ace!
Charlie|6|3|Triple bogey
Also check out: Extracting Domain Name from a URL in Python.
Conclusion
This is a basic golf score program in python. It is one of the most common programs for beginners in any programming language. Projects like this help us get familiar with the syntax of a language and also enhance our grip on the logic and control flow of a program. This program also gives us a taste of file management in python where we need to read and write from a file into our program. All in all, the golf scores program is worth giving a shot at!
What other sports-related programs could you create to further improve your Python skills?