import re import random file = open("input.txt","r") text = file.readlines() file.close() first = [] second = [] third = [] fourth = [] sentence =re.compile(r"(An?\s|The\s)?(\w+\s)(\w+\s)(an?\s|the\s)?(\w+\s)(.*\.)") for line in text: result = sentence.search (line) if result.group(1): first.append(result.group(1)+result.group(2)) else: first.append(result.group(2)) second.append(result.group(3)) if result.group(4): third.append(result.group(4)+result.group(5)) else: third.append(result.group(5)) fourth.append(result.group(6)) answer = raw_input("Would you like to see a random sentence? ") while answer != "no": print print first[random.randrange(0,3,1)]+second[random.randrange(0,3,1)] + \ third[random.randrange(0,3,1)] +fourth[random.randrange(0,3,1)] print answer = raw_input("Would you like to see a random sentence? ")