Python
10 Green Bottles
By Rob Poulter
# 10 Green Bottles in Python
# Initial number of bottles
num_bottles = 10
# To get the initial number from the user (with no error checking)
# uncomment the following line:
# num_bottles = input("How many bottles are on the wall? ")
# Loop until there are no bottles left
# Why did I use a while loop? Who knows. For loops people!
while (num_bottles > 0):
if (num_bottles == 1):
plural = "" # Don't include the 's' in bottles
bottles_left = "NO" # how many bottles left?
next_plural = "s" # is the next amount of bottles a plural?
else:
plural = "s"
bottles_left = str(num_bottles - 1) # change the number left next time to a string
if (num_bottles == 2):
next_plural = "" # if there are 2 bottles now there will be one bottle next time
else:
next_plural = "s"
print "%d green bottle%s, sitting on the wall!" % (num_bottles, plural)
print "And if one green bottle should accidentally fall"
print "There'd be %s green bottle%s sitting on the wall!" % (bottles_left, next_plural)
num_bottles -= 1 # Subtract one from the bottle count
Comments (0)
You don't have permission to comment on this page.