Python编程从入门到实践 10-11喜欢的数字

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

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import json

filename='number.json'
number=raw_input("Please input your favorite number: ")
with open(filename,'w') as file_object:
	json.dump(number,file_object)
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import json
filename='number.json'
with open(filename) as file_object:
	number=json.load(file_object)
	print("I konw your favorite number! It's "+number+" !")

结果:

Python编程从入门到实践 10-11喜欢的数字_第1张图片

你可能感兴趣的:(Python练习)