Hello all,
After about my 5th attempt to get interested in learning coding I've finally made the smallest amount of progress that has created some momentum in my learning. I'm interested in creating a simple(not really) golf app and have started with python.
So far what I have is python reading a CSV file that contains 2 columns of data. It creates a list for each column. Doing this I can ask for user input, look up the position of that user input in list 1, and then return the value in the corresponding position of list 2.
With that in mind, I've gotten as far as asking the user how long the hole he is about to play is, then asked him where the shot he hit finished. Using those inputs I can then calculate how good of a shot he hit.
Code:
import csv
#puts starting locations into a list
f = open('SGdata.csv')
csv_f = csv.reader(f)
shots = []
for row in csv_f:
shots.append(row[0])
f.close()
#puts strokes gained from certain locations into a list
f = open('SGdata.csv')
csv_f = csv.reader(f)
strokesgained = []
for row in csv_f:
strokesgained.append(row[1])
f.close()
#prints menu options and prompts for choice
def menu():
print "Welcome to the Strokes Gained Calculator"
print "your options are:"
print " "
print "1) Calculate Strokes Gained"
print "2)Exit the calculator"
print " "
return input ("Choose your option:")
#finish location menu
def finish_locations():
print "Where did your shot finish?"
print " "
print "'F' for Fairway"
print "'R' for Rough"
print "'S' for Sand"
print "'G' for Green"
print "'R' for Recovery"
print "'H' for In The Hole"
print " "
#calculates strokes gained for your tee shot
def StrokesGained():
hole_length = raw_input('Enter the length of the hole you are playing:')
t = 'T'
starting_shot = '%s-%s' %(t,hole_length)
Start = shots.index(starting_shot)
SGTHO = float(strokesgained[Start])
print "Average strokes to complete hole measuring " + str(hole_length) + " yards = " + str(SGTHO)
print " "
finish_locations()
finish_location = raw_input('Enter finish surface: ')
finish_distance = raw_input('How far from the hole did your shot finish? ')
finish_shot = '%s-%s' %(finish_location,finish_distance)
Finish = shots.index(finish_shot)
SG = float(strokesgained[Finish])
print "You gained ",SGTHO - SG - 1," shots"
print " "
loop = 1
choice = 0
while loop == 1:
choice = menu()
if choice == 1:
StrokesGained()
elif choice == 2:
loop = 0
print "Thank you for using calculator"
Quick example:
If you are playing a 400 yard par 4, it takes on average 3.99 shots to get your ball in the hole.
You hit a good shot, 300 yards down the fairway, so you are now 100 yards away in the fairway. From that location/distance it takes 2.8 shots to get your ball in the hole.
So to calculate how good the shot was you would do 3.99 - 2.8 - 1(for the shot it took you to get from point A to point B) = .19. This is currently what the code returns.
Where I'm getting stuck is how to do more than 1 shot at a time. How could I calculate it for an entire hole. I would need to keep asking the user where each of his shots finished, and then calculating that finish location with the previous starting location. And then of course it has to be dynamic(?) bc sometimes you only take 3 shots on a hole and sometimes you take 8. Anyone following? Kind of complicated I guess. Maybe how can I store each location/distance combo they enter?
Any help would be greatly appreciated.