10-11喜欢的数字 :编写一个程序,提示用户输入他喜欢的数字,并使用json.dump() 将这个数字存储到文件中。
# -*- coding: utf-8 -*-
import json
# 存储用户喜欢的数字
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
# 读取用户喜欢的数字
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记住喜欢的数字 :将练习10-11中的两个程序合而为一。如果存储了用户喜欢的数字,就向用户显示它,否则提示用户输入他喜欢的数字并将其存储到文件中。
运行这个程序两次,看看它是否像预期的那样工作。
方法一:
# -*- coding: utf-8 -*-
import json
# 如果以前存储有数字,就读取它
# 否则,提示用户输入它喜欢的数字并存储
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():
"""如果存储了数字,获取它"""
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():
"""提示用户输入喜欢的数字"""
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():
"""打印用户喜欢的数字"""
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() 中打印欢迎用户回来的消息前,先询问他用户名是否是对的。如果不对,就调用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