Python學習筆記之json.dump和json.load

10-11 喜歡的數字:編寫一個程序,提示用戶輸入他喜歡的數字,並使用json.dump()將這個數字存儲到文件中。再編寫一個程序,從文件中讀取這個值,並打印消息「I know your favorite number! It’s _____.」。json

favorite_number.pyspa

 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 中的兩個程序合而爲一。若是存儲了用戶喜歡的數字,就向用戶顯示它,不然提示用戶輸入他喜歡的數字並將其存儲到文件中。運行這個程序兩次,看看它是否像預期的那樣工做。code

 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 版本假設用戶要麼已輸入其用戶名,要麼是首次運行該程序。咱們應修改這個程序,以應對這樣的情形:當前和最後一次運行該程序的用戶並不是同一我的。blog

爲此,在greet_user()中打印歡迎用戶回來的消息前,先詢問他用戶名是不是對的。若是不對,就調用get_new_username()讓用戶輸入正確的用戶名。rem

原remember_me.py代碼get

 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代碼input

 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()
相關文章
相關標籤/搜索