-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_refactoring
More file actions
33 lines (29 loc) · 906 Bytes
/
code_refactoring
File metadata and controls
33 lines (29 loc) · 906 Bytes
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
# -*- coding:utf-8 -*-
import json
def get_stored_username():
'''如果存储了用户名,就从文件获取用户名'''
# filename = 'username.json'
try:
with open(filename) as file_read:
username = json.load(file_read)
except FileNotFoundError:
return None
else:
return username
def get_new_username():
'''提示用户输入用户名'''
username = input("What is your name?")
# filename = 'username.json'
with open(filename, 'w') as file_obj:
json.dump(username, file_obj)
return username
def greet_user():
'''问候用户并指出其姓名'''
username = get_stored_username()
if username:
print("Welcome back, " + username + " !")
else:
username = get_new_username()
print("We'll remember you when you come back, " + username + "!")
filename = 'username.json'
greet_user()