Repository navigation
Expand file tree
/
Copy pathrember.py
More file actions
56 lines (46 loc) · 1.94 KB
/
Copy pathrember.py
File metadata and controls
56 lines (46 loc) · 1.94 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
from pathlib import Path
import json
from write_and_read import write, read
def get_stored_user_info(path):
"""获取用户名称,向用户打招呼"""
user_info = read(path)
return user_info # 函数如果没有返回值,就没办法测试了
def get_new_user_info(path, name=None):
"""引导用户输入,存储用户名称、性别、年龄"""
if name: # 避免新用户重复输入两次姓名,莫名其妙😕
username = name
else:
username = input("What's your name? ").title()
gender = input("What's your gender? ")
age = input("What's your age? ")
user_info = {
"username": username,
"gender": gender,
"age": age,
}
write(path, user_info)
return user_info
def greet_user(path):
"""判断是否存在正确的用户名,有的话和用户打招呼,没有的话进行存储"""
name = input("What's your name ? ").title()
# Path(path).exists() will return bool to indicate whither the file exists
if Path(path).exists(): # 判断存储用户名称的文件是否存在
user_info = get_stored_user_info(path)
username = user_info["username"]
if name == username: # 判断登录的用户和存储的用户信息是否一致
print(f"Welcome back, {username}!") # 如果一致就读取
else: # 否则就存储新的用户信息
user_info = get_new_user_info(path, name)
username = user_info["username"]
gender = user_info["gender"]
age = user_info["age"]
print(
f"We'll remember your info when you com back: {username, gender, age}!"
)
else:
user_info = get_new_user_info(path)
username = user_info["username"]
gender = user_info["gender"]
age = user_info["age"]
print(f"We'll remember your info when you com back: {username, gender, age}!")
greet_user("./json_files/name.json")