-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfun_facts.py
62 lines (42 loc) · 1.39 KB
/
fun_facts.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
# -*- coding: utf-8 -*-
"""
A program to display some fun facts.
"""
# A function to help with the rest of the program:
def yorn(question):
"""Gets the user to answer a 'yes or no' question (YORN).
The function is 'forgiving' and allows:
* surrounding spaces
* lower or uppercase answers
Returns a boolean, so that the function can be used in an 'if' statement.
Example:
>>> yorn('Would you like a buttered crumpet?')
Would you like a buttered crumpet? (y/n) Y
True
Arguments:
question: A string prompt.
The additional prompt ' (y/n) ' will be appended.
Returns:
Boolean
"""
answer = ''
while answer not in ['y', 'n']:
answer = input(question + ' (y/n) ')
answer = answer.strip().lower()
if answer == 'y':
return True
else:
return False
# The program itself:
fun_facts = ['Elephants gestate for 21 months.',
'There are more tin cans than people.',
'The record number of marshmallows stuffed up one nostril is 604.']
if yorn('Would you like to hear some fun facts?'):
for fact in fun_facts:
if yorn('Would you like me to SHOUT so you can hear clearly?'):
fact = fact.upper()
print('Ok, here you go:')
print(fact)
if not yorn('Would you like to hear another?'):
break
print('Ok. Bye.')