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

  • Buried in cloud files? We can help with Spring cleaning!

    Whether you use Dropbox, Drive, G-Suite, OneDrive, Gmail, Slack, Notion, or all of the above, Dokkio will organize your files for you. Try Dokkio (from the makers of PBworks) for free today.

  • Dokkio (from the makers of PBworks) was #2 on Product Hunt! Check out what people are saying by clicking here.

View
 

10gbPythonTeacher

Page history last edited by Mike Leishman 15 years 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.