Python学习笔记之json.dump和json.load

10-11 喜欢的数字:编写一个程序,提示用户输入他喜欢的数字,并使用json.dump()将这个数字存储到文件中。再编写一个程序,从文件中读取这个值,并打印消息“I know your favorite number! It’s _____.”。

favorite_number.py

 1 import json
 2 
 3 def favorite_number():
 4     """提示用户输入喜欢的数字"""
 5     filename = 'favorite_number.json'
 6     number = input("请输入你喜欢的数字:")
 7     with open(filename, 'w') as f_obj:
 8         json.dump(number, f_obj)
 9 
10 favorite_number()
get_favorite_number.py
 1 import json
 2 
 3 def get_favorite_number():
 4     """获取文件内容"""
 5     filename = 'favorite_number.json'
 6     with open(filename) as f_obj:
 7         get_number = json.load(f_obj)
 8 
 9     print("I know your favorite number! It’s " + str(get_number) + ".")
10 
11 get_favorite_number()

 

10-12 记住喜欢的数字:将练习10-11 中的两个程序合而为一。如果存储了用户喜欢的数字,就向用户显示它,否则提示用户输入他喜欢的数字并将其存储到文件中。运行这个程序两次,看看它是否像预期的那样工作。

 1 import json
 2 
 3 def get_stored_number():
 4     filename = 'favorite_number.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 favorite_number():
14     """提示用户输入喜欢的数字"""
15     filename = 'favorite_number.json'
16     number = input("请输入你喜欢的数字:")
17     with open(filename, 'w') as f_obj:
18         json.dump(number, f_obj)
19     return number
20 
21 
22 def get_favorite_number():
23     """获取文件内容"""
24     # filename = 'favorite_number.json'
25     # with open(filename) as f_obj:
26     #     get_number = json.load(f_obj)
27     number = get_stored_number()
28     if number:
29         print("I know your favorite number! It’s " + str(number) + ".")
30     else:
31         number = favorite_number()
32         print("I know your favorite number! It’s " + str(number) + ".")
33 
34 get_favorite_number()

 

10-13 验证用户:最后一个remember_me.py 版本假设用户要么已输入其用户名,要么是首次运行该程序。我们应修改这个程序,以应对这样的情形:当前和最后一次运行该程序的用户并非同一个人。

为此,在greet_user()中打印欢迎用户回来的消息前,先询问他用户名是否是对的。如果不对,就调用get_new_username()让用户输入正确的用户名。

原remember_me.py代码

 1 import json
 2 
 3 def get_stored_username():
 4     """如果存储了用户名,就获取它"""
 5     filename = 'username.json'
 6     try:
 7         with open(filename) as f_obj:
 8             username = json.load(f_obj)
 9     except FileNotFoundError:
10         return None
11     else:
12         return username
13 
14 def get_new_username():
15     """提示用户输入用户名"""
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
21 
22 def greet_user():
23     """问候用户,并指出其名字"""
24     username = get_stored_username()
25     if username:
26         print("Welcome come back, " + username + "!")
27     else:
28         username = get_new_username()
29         print("We will remember you when you come back, " + username + "!")
30 
31 greet_user()

修改之后remember_me.py代码

 1 import json
 2 
 3 def get_stored_username():
 4     """如果存储了用户名,就获取它"""
 5     filename = 'username.json'
 6     try:
 7         with open(filename) as f_obj:
 8             username = json.load(f_obj)
 9     except FileNotFoundError:
10         return None
11     else:
12         return username
13 
14 def get_new_username():
15     """提示用户输入用户名"""
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
21 
22 def greet_user():
23     """问候用户,并指出其名字"""
24     username = get_stored_username()
25     if username:
26         msg = "Are you " + username + "? (y/n) "
27         correct = input(msg)
28         if correct == 'y':
29             print("Welcome come back, " + username + "!")
30         else:
31             username = get_new_username()
32             print("We will remember you when you come back, " + username + "!")
33 
34     else:
35         username = get_new_username()
36         print("We will remember you when you come back, " + username + "!")
37 
38 greet_user()

 

posted @ 2019-10-31 15:39  雨夜Rainight  阅读(5250)  评论(0编辑  收藏  举报