def get_numquotes(): """ Return number of quotes in quotes """ global numquotes # This will only be True the first time. # On following calls numquotes will be # returned immediately. if numquotes == None: numquotes = 0 for line in quotes.split('\n'): line.strip() if (not line.startswith('#')) and (':' in line): numquotes += 1 return numquotes
def get_quote(): """ Choose quote number n at random """ n = random.randrange(get_numquotes()) i = 0 for line in quotes.split('\n'): line.strip() if (not line.startswith('#')) and (':' in line): if i == n: break else: i += 1 # Now line is the quote we want # Split it into a category and a quote part category, quote = line.split(':') category = category.strip() quote = quote.strip() return (category, quote)