10-11喜歡的數(shù)字 :編寫一個程序二驰,提示用戶輸入他喜歡的數(shù)字瑞妇,并使用json.dump() 將這個數(shù)字存儲到文件中稿静。
# -*- coding: utf-8 -*-
import json
# 存儲用戶喜歡的數(shù)字
filename = "number.json"
number = raw_input("What is your favorite number: ")
with open(filename,"w") as f_obj:
json.dump(number,f_obj)
print("Write success!")
結果:
What is your favorite number: 23
Write success!
Process finished with exit code 0
再編寫一個程序,從文件中讀取這個值辕狰,并打印消息“I know your favorite number! It's _____.”改备。
# -*- coding: utf-8 -*-
import json
# 讀取用戶喜歡的數(shù)字
filename = "number.json"
with open(filename) as f_obj:
number = json.load(f_obj)
print("I know your favorite number!It's " + number + ".")
結果:
I know your favorite number!It's 23.
Process finished with exit code 0
10-12記住喜歡的數(shù)字 :將練習10-11中的兩個程序合而為一。如果存儲了用戶喜歡的數(shù)字柳琢,就向用戶顯示它绍妨,否則提示用戶輸入他喜歡的數(shù)字并將其存儲到文件中。
運行這個程序兩次柬脸,看看它是否像預期的那樣工作。
方法一:
# -*- coding: utf-8 -*-
import json
# 如果以前存儲有數(shù)字毙驯,就讀取它
# 否則倒堕,提示用戶輸入它喜歡的數(shù)字并存儲
filename = "number.json"
try:
with open(filename) as f_obj:
number = json.load(f_obj)
except IOError:
number = raw_input("What is your favorite number: ")
with open(filename,"w") as f_obj:
json.dump(number,f_obj)
else:
print("I know your favorite number!It's " + number + ".")
第一次運行結果:
What is your favorite number: 2323
Process finished with exit code 0
第二次運行結果:
I know your favorite number!It's 2323.
Process finished with exit code 0
方法二:
# -*- coding: utf-8 -*-
import json
def get_number():
"""如果存儲了數(shù)字,獲取它"""
filename = "number.json"
try:
with open(filename) as f_obj:
number = json.load(f_obj)
except IOError:
return None
else:
return number
def write_number():
"""提示用戶輸入喜歡的數(shù)字"""
filename = "number.json"
number = raw_input("What is your favorite number: ")
with open(filename,"w") as f_obj:
json.dump(number,f_obj)
return number
def print_number():
"""打印用戶喜歡的數(shù)字"""
number = get_number()
if number:
print("I know your favorite number!It's " + number + ".")
else:
number = write_number()
print("I'll keep in mind that your favorite number is " + number)
print_number()
第一次運行結果:
What is your favorite number: 2323
I'll keep in mind that your favorite number is 2323
Process finished with exit code 0
第二次運行結果:
I know your favorite number!It's 2323.
Process finished with exit code 0
10-13驗證用戶 :最后一個remember_me.py版本假設用戶要么已輸入其用戶名爆价,要么是首次運行該程序垦巴。我們應修改這個程序,以應對這樣的情形:當前和最后一次運行該程序的用戶并非同一個人铭段。
為此骤宣,在greet_user() 中打印歡迎用戶回來的消息前,先詢問他用戶名是否是對的序愚。如果不對憔披,就調(diào)用get_new_username() 讓用戶輸入正確的用戶名。
代碼如下:
# -*- coding: utf-8 -*-
import json
def get_stored_username():
"""如果存儲了用戶名爸吮,就獲取它"""
filename = "username.json"
try:
with open(filename) as f_obj:
username = json.load(f_obj)
except IOError:
return None
else:
return username
def get_new_username():
"""提示用戶輸入用戶名"""
username = raw_input("What is your name: ")
filename = "username.json"
with open(filename,'w') as f_obj:
json.dump(username,f_obj)
return username
def greet_user():
"""問候用戶芬膝,并指出其名字"""
username = get_stored_username()
if username:
self_name = raw_input(username + " is your name?(yes/ no)\n ")
if self_name == "yes":
print("Welcome back, " + username + "!")
else:
username = get_new_username()
print("I'll remember you when you come back, " + username + "!")
else:
username = get_new_username()
print("I'll remember you when you come back, " + username + "!")
greet_user()
驗證代碼:
What is your name: leilei
I'll remember you when you come back, leilei!
Process finished with exit code 0
leilei is your name?(yes/ no)
no
What is your name: 23
I'll remember you when you come back, 23!
Process finished with exit code 0
23 is your name?(yes/ no)
yes
Welcome back, 23!
Process finished with exit code 0