| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!

View
 

10gbPythonTeacher

Page history last edited by Mike Leishman 16 years, 10 months ago

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.