-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun.py
More file actions
265 lines (211 loc) · 7.13 KB
/
Copy pathrun.py
File metadata and controls
265 lines (211 loc) · 7.13 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import gspread
from google.oauth2.service_account import Credentials
import textwrap
import os
from time import sleep
SCOPE = [
"https://www.googleapis.com/auth/spreadsheets",
"https://www.googleapis.com/auth/drive.file",
"https://www.googleapis.com/auth/drive"
]
CREDS = Credentials.from_service_account_file("creds.json")
SCOPED_CREDS = CREDS.with_scopes(SCOPE)
GSPREAD_CLIENT = gspread.authorize(SCOPED_CREDS)
SHEET = GSPREAD_CLIENT.open("bedtime_adventures")
def get_welcome_message():
"""
Get the welcome message and print it.
"""
story = SHEET.worksheet("welcome").get_all_values()
story_text = "\n".join([row[0] for row in story])
story_text = story_text.replace("'\\n'", "\n")
story_text = wrap_text(story_text)
print(story_text)
def get_user_name1():
"""
Get name input from user and will loop if not passed through
validator.
"""
print("Who will be the heroes of tonight's adventure? We need two brave")
print("and adventurous names for our characters. \n")
while True:
print("Please type in a name and press enter! \n")
name1 = input("Enter the name of the first hero here:\n")
name1 = name1.capitalize()
if validate_names(name1):
print(f"Hello {name1}! \n")
break
else:
continue
return name1
def get_user_name2():
"""
Get second name input from user and will loop if not passed
through validator.
"""
sleep(1)
while True:
name2 = input("Enter the name of the second hero here:\n")
name2 = name2.capitalize()
if validate_names(name2):
print(f"Hello to you too, {name2}. Let's start our adventure! \n")
break
return name2
def validate_names(name):
"""
Check for a name with 3 or more letters.
Check for only letters.
"""
try:
if len(name) < 3:
raise ValueError(
f"We need a name with at least 3 letters from"
"you and you gave us {len(name)}!"
)
for letter in name:
if not letter.isalpha():
raise ValueError(
"Looks like we can only accept letters from A to Z."
"Please make sure to only enter characters"
"from the alphabet"
)
except ValueError as e:
print(f"Oopsie daisy! {e}. Try again!\n")
return False
return True
def wrap_text(text):
"""
Wrap text so lines will not be longer than 70 characters.
"""
wrapper = textwrap.TextWrapper(width=70, replace_whitespace=False)
story_text = wrapper.fill(text=text)
return story_text
def get_start_story(name1, name2):
"""
Get the beginning of the story from google sheets and replace [Name1]
and [Name2] with names from input. Add adventure choice for user
to pick how the story continues.
"""
sleep(2)
os.system('clear')
story = SHEET.worksheet("story").get_all_values()
story_text = "\n".join([row[0] for row in story])
story_text = story_text.replace("[Name1]", name1)
story_text = story_text.replace("[Name2]", name2)
story_text = story_text.replace("'\\n'", "\n")
story_text = wrap_text(story_text)
print(story_text)
while True:
choice1 = input("Insert X or Y here:\n")
choice1 = choice1.capitalize()
if validate_story_choice(choice1):
print("Let's go! \n")
break
return choice1
def get_adventure_story(name1, name2, choice1):
"""
Get adventure story X or Y depending on choice in start story.
Add end choice for user to pick how the story ends.
"""
sleep(2)
os.system('clear')
if choice1 == "X":
story = SHEET.worksheet("x").get_all_values()
elif choice1 == "Y":
story = SHEET.worksheet("y").get_all_values()
story_text = "\n".join([row[0] for row in story])
story_text = story_text.replace("[Name1]", name1)
story_text = story_text.replace("[Name2]", name2)
story_text = story_text.replace("'\\n'", "\n")
story_text = wrap_text(story_text)
print(story_text)
while True:
choice2 = input("Insert X or Y here:\n")
choice2 = choice2.capitalize()
if validate_story_choice(choice2):
print("Let's go! \n")
break
return choice1 + choice2
def get_end_story(name1, name2, choice2):
"""
Get adventure story XX, XY, YX or Y depending on choice in adventure story.
Add ending.
"""
sleep(2)
os.system('clear')
if choice2 == "XX":
story = SHEET.worksheet("xx").get_all_values()
elif choice2 == "XY":
story = SHEET.worksheet("xy").get_all_values()
elif choice2 == "YX":
story = SHEET.worksheet("yx").get_all_values()
elif choice2 == "YY":
story = SHEET.worksheet("yy").get_all_values()
story_text = "\n".join([row[0] for row in story])
story_text = story_text.replace("[Name1]", name1)
story_text = story_text.replace("[Name2]", name2)
story_text = story_text.replace("'\\n'", "\n")
story_text = wrap_text(story_text)
print(story_text)
def get_ending(name1, name2):
"""
Get the end of the story from google sheets and replace [Name1] and [Name2]
with names from input.
"""
story = SHEET.worksheet("end").get_all_values()
story_text = "\n".join([row[0] for row in story])
story_text = story_text.replace("[Name1]", name1)
story_text = story_text.replace("[Name2]", name2)
story_text = story_text.replace("'\\n'", "\n")
story_text = wrap_text(story_text)
print(story_text)
while True:
choice3 = input("Insert X or Y here:\n")
choice3 = choice3.capitalize()
if validate_story_choice(choice3):
print("Let's go! \n")
break
return choice3
def get_final_page(name1, name2, choice3):
"""
The final part of story
"""
sleep(2)
os.system('clear')
if choice3 == "X":
story = SHEET.worksheet("endx").get_all_values()
elif choice3 == "Y":
story = SHEET.worksheet("endy").get_all_values()
story_text = "\n".join([row[0] for row in story])
story_text = story_text.replace("[Name1]", name1)
story_text = story_text.replace("[Name2]", name2)
story_text = story_text.replace("'\\n'", "\n")
story_text = wrap_text(story_text)
print(story_text)
def validate_story_choice(choice):
"""
Validate that user enters either X or Y
"""
try:
if choice != "X" and choice != "Y":
raise ValueError(
f"You need to pick either X or Y, you wrote {choice} !"
)
except ValueError as e:
print(f"Oopsie daisy! {e}. Try again!\n")
return False
return True
def main():
"""
Will run all everything
"""
get_welcome_message()
name1 = get_user_name1()
name2 = get_user_name2()
choice1 = get_start_story(name1, name2)
choice2 = get_adventure_story(name1, name2, choice1)
get_end_story(name1, name2, choice2)
choice3 = get_ending(name1, name2)
get_final_page(name1, name2, choice3)
print("Welcome to Bedtime Adventures! \n")
main()