Python练习题 10-13 验证用户

来源:互联网 发布:sqlserver恢复表数据 编辑:程序博客网 时间:2024/06/03 11:11

10-13 验证用户:最后一个 remember_me.py 版本假设用户要么已输入其用户名,要么是首次运行该程序。我们应修改这个程序,以应对这样的情形:当前和最后一次运行
该程序的用户并非同一个人。
为此,在 greet_user()中打印欢迎用户回来的消息前,先询问他用户名是否是对的。
如果不对,就调用 get_new_username()让用户输入正确的用户名。

#coding:utf-8import jsonmessage='no message!'def new_user():    NameJudge=input('Please Enter Your Name :')    filename=NameJudge+'.json'    with open(filename,'w') as f_obj:         json.dump(NameJudge,f_obj)def greet_user():    NameJudge=input('Please Enter Your Load Name :')    filename=NameJudge+'.json'    try:        with open(filename) as f_obj:            User=json.load(f_obj)    except FileNotFoundError:        print(message)    else:         print('Welcome home, '+User) judgment=input("Do you have the Accounts ?  Y/N\n")if judgment.title()=='Y':    greet_user()if judgment.title()=='N':    new_user()

运行结果:

Do you have the Accounts ?  Y/NNPlease Enter Your Name :Tony------------------(program exited with code: 0)请按任意键继续. . .
Do you have the Accounts ?  Y/NYPlease Enter Your Load Name :StarkWelcome home, Stark------------------(program exited with code: 0)请按任意键继续. . .

有个问题,就是中文不能解码。之前关于除法运算那道题是可以解码的啊!

这个问题现在已经解决了,大家可以看看这篇文章
http://blog.csdn.net/trochiluses/article/details/16825269