Created
April 8, 2016 21:22
-
-
Save blackknight36/7cf9daa7ae6e5ccfd28536356eae5ce4 to your computer and use it in GitHub Desktop.
How many beers can you drink?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
def drink_beer(): | |
global caps | |
global empties | |
global full | |
caps += 1 | |
empties += 1 | |
full -= 1 | |
print "Drank full bottle.\n" | |
def trade_empties(): | |
global caps | |
global empties | |
global full | |
global total | |
empties -= 2 | |
full += 1 | |
total += 1 | |
print "Traded in two empty bottles.\n" | |
def trade_caps(): | |
global caps | |
global empties | |
global full | |
global total | |
caps -= 4 | |
full +=1 | |
total += 1 | |
print "Traded in four bottle caps.\n" | |
# start with 5 full, unopened bottles (10/2) | |
empties = 0 | |
full = 5 | |
caps = 0 | |
total = 5 | |
while (1): | |
while (empties >= 2): | |
trade_empties() | |
while (caps >= 4): | |
trade_caps() | |
drink_beer() | |
print "empty bottles:", empties | |
print "caps held:", caps | |
print "Total bottles:", total, "\n" | |
if (empties < 2 and caps < 4 and full <= 0): | |
print "Nothing to trade or drink. Passing out.\n\n" | |
exit(0) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment