【python】练习 10.14:验证⽤户 最后⼀个 remember_me.py 版本假设⽤户要么 已输⼊其⽤户名,要么是⾸次运⾏该程序。

练习 10.14:验证⽤户 最后⼀个 remember_me.py 版本假设⽤户要么 已输⼊其⽤户名,要么是⾸次运⾏该程序。我们应修改这个程序,以 防当前⽤户并⾮上次运⾏该程序的⽤户。

要求

为此,在 greet_user() 中打印欢迎⽤户回来的消息之前,询问他⽤户名是否是对的。如果不对,就调⽤ get_new_username() 让⽤户输⼊正确的⽤户名。


代码

from pathlib import Path
import json


def get_stored_username(path):
    """如果存储了⽤户名,就获取它"""
    if path.exists():
        contents = path.read_text()
        username = json.loads(contents)
        return username['username']
    else:
        return None


def get_new_username(path):
    """获取用户名以判断是否相符"""
    username = input("What is your name?: ")
    return username


def get_new_user_info(path):
    """提⽰⽤户输⼊用户信息"""
    user_info = {}
    username = input("What is your name?: ")
    user_info['username'] = username
    age = input("How old are you?: ")
    user_info['age'] = age
    fav_food = input("What food you like?: ")
    user_info['favorite food'] = fav_food

    contents = json.dumps(user_info)
    path.write_text(contents)
    return username


def greet_user():
    """问候⽤户,并指出其名字"""
    path = Path('username.json')
    new_name = get_new_username(path)
    username = get_stored_username(path)
    if new_name == username:
        print("it's right")
        if username:
            print(f"Welcome back, {username}!")
        else:
            username = get_new_user_info(path)
            print(f"We'll remember you when you come back, {username}!")
    else:
        print("username is wrong")
        greet_user()


def get_info():
    """获取用户信息"""
    path = Path('username.json')
    contents = path.read_text()
    infos = json.loads(contents)
    for key, value in infos.items():
        print(f"{key}:{value}")
    return infos


def judgment_user(path):
    """判断用户是谁"""
    path = Path('username.json')
    username = get_stored_username(path)


greet_user()
get_info()

打印

请添加图片描述

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-20 04:44:02       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-20 04:44:02       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-20 04:44:02       45 阅读
  4. Python语言-面向对象

    2024-07-20 04:44:02       55 阅读

热门阅读

  1. 3507软件IIC,IO输入输出函数

    2024-07-20 04:44:02       15 阅读
  2. Elasticsearch扩容与缩容集群

    2024-07-20 04:44:02       18 阅读
  3. 三、模型转换和压缩扩写

    2024-07-20 04:44:02       15 阅读
  4. python实现动态规划算法

    2024-07-20 04:44:02       19 阅读
  5. 分布式锁的最佳实践之Redisson

    2024-07-20 04:44:02       19 阅读
  6. [沫忘录]Golang基础类型与语法

    2024-07-20 04:44:02       19 阅读
  7. python需要掌握那些语法

    2024-07-20 04:44:02       17 阅读
  8. 关于shell的变量替换

    2024-07-20 04:44:02       18 阅读
  9. JVM--内存分配与回收策略

    2024-07-20 04:44:02       17 阅读
  10. autosar mcal I2C

    2024-07-20 04:44:02       15 阅读
  11. 关于防重,我是这么设计的

    2024-07-20 04:44:02       15 阅读
  12. python实现计数排序、桶排序和基数排序算法

    2024-07-20 04:44:02       14 阅读
  13. Carousel of Combinations

    2024-07-20 04:44:02       16 阅读
  14. VUE Pinia和Vuex的比较

    2024-07-20 04:44:02       17 阅读