import random
class PhraseBank:
def __init__(self,fname):
self.phrases = {}
with open(fname) as f:
data = f.read().splitlines()
for line in data:
if line.startswith("**"):
topic = line[2:].upper()
self.phrases[topic] = []
else:
self.phrases[topic].append(line.upper())
def next_phrase(self,topic):
i = random.randint(0,len(self.phrases[topic]))
return self.phrases[topic][i]
def get_all_topics(self):
return list(self.phrases.keys())
#result = []
#for topic in self.phrases:
# result.append(topic)
#return result
pb = PhraseBank("p.txt")
print(pb.phrases)
print(pb.get_all_topics())
{'MOVIE': ['THE SHAWSHANK REDEMPTION ', 'THE GODFATHER ', 'PULP FICTION ', 'SCHINDLERS LIST '], 'COUNTRY NAME': ['AFGHANISTAN', 'ALBANIA', 'ALGERIA', 'ANDORRA'], 'NBA TEAM': ['BOSTON CELTICS', 'BROOKLYN NETS', 'NEW YORK KNICKS']} ['MOVIE', 'COUNTRY NAME', 'NBA TEAM']
import random
phrases = ["Hello","Bye","How","are","you"]
print(phrases)
random.shuffle(phrases)
print(phrases)
['Hello', 'Bye', 'How', 'are', 'you'] ['How', 'Bye', 'you', 'are', 'Hello']