# sing # # this silly function is given an integer value and prints the verse of # "99 Bottles of Beer" that begins with that number # def sing(number): start = str(number) end = str(number-1) print "\n" + start + " bottles of beer on the wall, " print start + " bottles of beer." print "Take one down, and pass it around, " print end + " bottles of beer on the wall. " # getInteger # # This function is given a string prompt to use for an input statement # and solicits a number from the user using that prompt. # # If the value entered is an integer, the function returns it. Otherwise # it requests that the user try again. # def getInteger(prompt): badNumber = True while badNumber: badNumber = False # assume good input candidate = raw_input (prompt) for i in range (len(candidate)): if candidate[i] < "0" or candidate [i] > "9": # it's not numeric badNumber = True if badNumber: # need to ask for another print "You entered " + candidate candidate = raw_input (prompt) return int(candidate) # return the number to caller # control # # This function asks the user to enter a number to start singing # the song, and then calls the function repeatedly until # the number zero is reached (or the user kills the process!) # def control(): print "Let's sing '999 bottles!\n" verseNumber = getInteger("Enter the verse you want to start on>> ") for verse in range (verseNumber, 0, -1): sing(verse) print "\n\n Ta-dah!" control()