Skip to content

Commit 6560d22

Browse files
committed
2019-09-26 at school
1 parent 433dcce commit 6560d22

File tree

4 files changed

+72
-13
lines changed

4 files changed

+72
-13
lines changed

Chapter10/favorite.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"12"

Chapter10/favorite_number.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import json
2+
3+
def get_favorite_number():
4+
filename = "favorite.json"
5+
try:
6+
with open(filename) as f_obj:
7+
number = json.load(f_obj)
8+
except FileNotFoundError:
9+
return None
10+
else:
11+
return number
12+
13+
def get_new_number():
14+
filename = "favorite.json"
15+
# ask client's favorite number
16+
number = input("Please input your favorite number: ")
17+
18+
# save client's favoirte number
19+
with open(filename, "w") as f_obj:
20+
json.dump(number, f_obj)
21+
22+
def get_user_number():
23+
number = get_favorite_number()
24+
if number:
25+
output_number(number)
26+
else:
27+
get_new_number()
28+
29+
def output_number(number):
30+
# output client's favorite number
31+
print("I know your favorite number! It's " + str(number))
32+
33+
get_user_number()

Chapter10/remember_me.py

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,47 @@
11
import json
22

3-
def greet_user():
4-
# if you saved data, load it.
5-
# Or informe your user to add his/her name.
6-
"""greet user, and print his/her name"""
7-
filename = 'username.json'
8-
try:
3+
def get_stored_username():
4+
"""if you saved username, print it"""
5+
filename = "username.json"
6+
try:
97
with open(filename) as f_obj:
108
username = json.load(f_obj)
119
except FileNotFoundError:
12-
username = input("What is your name? ")
13-
with open(filename, 'w') as f_obj:
14-
json.dump(username, f_obj)
15-
print("We'll remember you when you come back, " +
16-
username + "!")
10+
return None
1711
else:
18-
print("Welcome back, " + username + "!")
12+
return username
13+
14+
def get_new_username():
15+
"""inform user to input his/her name"""
16+
username = input("What is your name? ")
17+
filename = "username.json"
18+
with open(filename, "w") as f_obj:
19+
json.dump(username, f_obj)
20+
return username
1921

22+
def ask_user(username):
23+
if username == None:
24+
return None
25+
while True:
26+
msg = "Are you " + username + "? (yes/no) "
27+
answer = input(msg)
28+
if answer == "yes":
29+
return username
30+
elif answer == "no":
31+
return None
32+
33+
def greet_user():
34+
# if you saved data, load it.
35+
# Or informe your user to add his/her name.
36+
"""greet user, and print his/her name"""
37+
username = get_stored_username()
38+
username = ask_user(username)
39+
if username:
40+
print("Welcome back, " + username + "!")
41+
else:
42+
username = get_new_username()
43+
print("We'll remember you when you come back, " + username + "!")
44+
2045
greet_user()
2146

2247

Chapter10/username.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
"Trump"
1+
"shunhou"

0 commit comments

Comments
 (0)