Python基础学习_文件和异常_模块json习题

1-11 喜欢的数字

import json

写的文件

filename = 'favorite_numbers.json'

love_number = input("What are you favorite number are? ")
with open(filename,'w') as f_obj:
    json.dump(love_number, f_obj)
    print("The number is " + love_number + ".")

结果:

"999999"

读的文件

import json

filename = "favorite_numbers.json"
with open(filename) as f_obj:
    love_number = json.load(f_obj)
    print("I know your favorite number! " + "It's " + love_number + "." ) 

结果:

I know your favorite number! It's 999999.

1-12 记住喜欢的数字 合并前两个程序

import json

filename = "favorite_numbers.json"
try:
    with open(filename) as f_obj:
         love_number = json.load(f_obj)
except FileNotFoundError:
    love_number = input("What are you favorite number are? ")
    with open(filename,'w') as f_obj:
        json.dump(love_number, f_obj)
        print("The number is " + love_number + "." )
else:
    print("I know your favorite number! " + "It's " + love_number + "." )

结果:

I know your favorite number! It's 999999.

封装为函数

import json

def get_favorite_number():
    """问用户最喜欢的数字,并显示这个数字。"""
    filename = "favorite_numbers.json"
    
    try:
        with open(filename) as f_obj:   ## f_obj为文件对象的变量名
        ## json.load()中只需要放入文件对象的变量名即可
            love_number = json.load(f_obj)    
    except FileNotFoundError:
        love_number = input("What are you favorite number is ? ")
        with open(filename,'w') as f_obj:
            json.dump(love_number, f_obj)
            print("The number are " +  love_number + ".")
    else:
        print("I know your favorite number! " + "It's " + love_number + ".")

get_favorite_number()

结果:

get_favorite_number()
I know your favorite number! It's 999999.

重构

import json

def get_favorite_number_sorted():
    """获得数字,并显示数字"""
    filename = "favorite_numbers.json"
    try:
        with open(filename) as f_obj:       ## f_obj为文件对象的变量名
         ## json.load()中只需要放入文件对象的变量名即可
            love_number = json.load(f_obj)
    except FileNotFoundError:
        return None
    else:
        return love_number

def get_new_favorite_number():
    """问候用户,获得新的喜欢的数字"""
    filename = "favorite_numbers.json"
    love_number = "What are you favorite number are ?"
    
    with open(filename,'w') as f_obj:
        json.dump(love_number, f_obj)
        print("The number are " + love_number + ".")
    return love_number

def get_favorite_number():
    """问候用户喜欢的数字,并打印"""
    love_number = get_favorite_number_sorted()
    
    if love_number:
        print("I know your favorite number! " + "It's " + love_number + ".")
    else:
        love_number = get_new_favorite_number()
        print("The number is " + love_number + ".")

get_favorite_number()

结果:

I know your favorite number! It's 999999.

1-13 验证用户

即验证用户名是否与已经存在的用户名是否一样,一样则欢迎,不一样重新输入

def get_sorted_username():
    """如果存储了用户名,就获取它"""
    filename = 'username.json'
    
    try:
        with open(filename) as f_obj:
            username = json.load(f_obj)
    except FileNotFoundError:
        return None
    else:
        return username

def get_new_username():
    """提示用户输入用户名"""
    username = 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():
    """问候用户,并指出其名字"""
    flag = True
    while flag:
        usernames = input("Please enter your name? ")
        if usernames == get_sorted_username():
            username = get_sorted_username()
            flag = False
        else:
            flag = True
            
    
    if username:
        print("Welcome back, " + username + "!")
    else:
        username = get_new_username()
        print("We'll remember you when you come back, " + username + "!")

greet_user()               

结果:

Please enter your name? liming
Welcome back, liming!

案例中文件为

username.json

"liming"

favorite_numbers.json

"999999"

你可能感兴趣的:(python习题,python,学习,json)