Example Python Script – Calculating ABV percentage for beer or wine
Hi everyone,
I have been playing around with Python on my Mac OSX Lion, version 2.7. (If you need to confirm the version just type using your Terminal:
$ Python -version
I came up with this handy script to check the ABV (alcohol percentage) for Beer or Wine. It’s a handy script if you’re into home brewing. It also helped get me going on how simple it is to use Python and it’s also handy for prototyping different algorithms before getting to some serious coding in another language!
By the way, you may use this script without restrictions, but if I ever meet you buy me a beer, preferably a DIPA :=)).
/*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return Poul-Henning Kamp
* ----------------------------------------------------------------------------
*/
# Program used to calculate the alcohol percentage by volume in beer.
# @ Greg Werner
# Variable definitions for command prompt inputs. Includes conversion from strings to floats
og = raw_input('Please enter your Original Gravity reading: ')
o = float(og)
tempog = raw_input('Please enter the temperature, in F, when obtaining your Original Gravity: ')
tempOG = float(tempog)
fg = raw_input('Please enter your Final Gravity reading: ')
f = float(fg)
tempfg = raw_input('Please enter the temperature, in F, when obtaining your Final Gravity: ')
tempFG = float(tempog)
# Function definition for original gravity and final gravity parameters with three floating point positions.
def abv(o, f):
x = '%.3f' % (o)
y = '%.3f' % (f)
og = float(x)
fg = float(y)
# Conditions for adjusting original y final gravity readings according to temperature in F.
if tempFG <= 69:
fg = fg + 0
elif tempFG >= 70 and tempFG <= 76:
fg = fg + 0.001
elif tempFG >= 77 and tempFG <= 83:
fg = fg + 0.002
elif tempFG >= 84 and tempFG <= 94:
fg = fg + 0.003
elif tempFG >= 95 and tempFG <= 104:
fg = fg + 0.004
else:
fg == 105
fg = tempFG + 0.007
if tempOG <= 69:
og = og + 0
elif tempOG >= 70 and tempOG <= 76:
og = og + 0.001
elif tempOG >= 77 and tempOG <= 83:
og = og + 0.002
elif tempOG >= 84 and tempOG <= 94:
og = og + 0.003
elif tempOG >= 95 and tempOG <= 104:
og = og + 0.004
else:
og == 105
og = og + 0.007
abv = ((1.05 * (og - fg))/ fg) / 0.79 * 100
return '%.3f' % abv
ABV = float(abv(o, f))
# Print results for ABV calculation.
if ABV <= 5.00:
print ('Your beer has an alcohol content of ' + str(ABV) +'. You have a low alcohol beer.')
elif ABV >= 5.01 and ABV <= 8.00:
print ('Your beer has an alcohol content of ' + str(ABV) +'. Drink in moderation, you have a medium alcohol beer.')
else:
print ('Your beer has an alcohol content of ' + str(ABV) +'. Be careful, you have yourself a bomb!')
Outstanding story there. What happened after?
Good luck!
Good question. I got super busy with work. I’ll start posting again though! Thanks for the note hope all is well.