-
Notifications
You must be signed in to change notification settings - Fork 127
/
san_antonio.py
78 lines (61 loc) · 2 KB
/
san_antonio.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# -*- coding: utf-8 -*-
import json
import random
# Give a Json file and return a List
def read_values_from_json(path, key):
values = []
with open(path) as f:
data = json.load(f)
for entry in data:
values.append(entry[key])
return values
# Give a json and return a list
def clean_strings(sentences):
cleaned = []
# Store quotes on a list. Create an empty list and add each sentence one by one.
for sentence in sentences:
# Clean quotes from whitespace and so on
clean_sentence = sentence.strip()
# don't use extend as it adds each letter one by one!
cleaned.append(clean_sentence)
return cleaned
# Return a random item in a list
def random_item_in(object_list):
rand_numb = random.randint(0, len(object_list) - 1)
return object_list[rand_numb]
# Return a random value from a json file
def random_value(source_path, key):
all_values = read_values_from_json(source_path, key)
clean_values = clean_strings(all_values)
return random_item_in(clean_values)
#####################
###### QUOTES #######
#####################
# Gather quotes from San Antonio
def random_quote():
return random_value('quotes.json', 'quote')
######################
#### CHARACTERS ######
######################
# Gather characters from Wikipedia
def random_character():
return random_value('characters.json', 'character')
######################
#### INTERACTION ######
######################
# Print a random sentence.
def print_random_sentence():
rand_quote = random_quote()
rand_character = random_character()
print(">>>> {} a dit : {}".format(rand_character, rand_quote))
def main_loop():
while True:
print_random_sentence()
message = ('Voulez-vous voir une autre citation ? '
'Pour sortir du programme, tapez [B].')
choice = input(message).upper()
if choice == 'B':
break
# This will stop the loop!
if __name__ == '__main__':
main_loop()