鱼C论坛

 找回密码
 立即注册
查看: 1865|回复: 2

[已解决]如何验证用户是否存在?

[复制链接]
发表于 2021-5-8 22:41:28 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. import json


  2. def get_stored_username():
  3.     filename = 'username.json'
  4.     try:
  5.         with open(filename) as f:
  6.             username = json.load(f)
  7.     except FileNotFoundError:
  8.         return None
  9.     else:
  10.         return username


  11. def get_new_username():
  12.     username = input('What is your name? ')
  13.     file = 'username.json'
  14.     with open(file, 'w') as f:
  15.         json.dump(username, f)
  16.     return username


  17. def greet_user():
  18.     username = get_stored_username()
  19.     if username:
  20.         answer = input(f"Are you jack?y/n ")
  21.         if answer == 'y':
  22.             print(f'welcome back {username}!')
  23.         else:
  24.             username = get_new_username()
  25.             print(f"We'll remember you when you back,{username}!")
  26.     else:
  27.         username = get_new_username()
  28.         print(f"We'll remember you when you back,{username}!")


  29. greet_user()
复制代码



这个程序主要作用就是验证用户是否存在,存在就问候,不存在就提示他,唐他输入用户名,存为老用户。
有一个问题就是,如果已经存在了老用户,然后输入的名字不是老用户的名字,然后提示你输入新用户,
此时,老用户的名字就被覆盖了,如何才能不覆盖,即使新用户注册了,下次验证时都能验证上。老用户不会消失。
最佳答案
2021-5-9 08:10:19
使用列表嵌套字典,或者使用数据库
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-5-9 08:10:19 From FishC Mobile | 显示全部楼层    本楼为最佳答案   
使用列表嵌套字典,或者使用数据库
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-5-9 22:33:48 | 显示全部楼层
wp231957 发表于 2021-5-9 08:10
使用列表嵌套字典,或者使用数据库

我是一个新手,不打明白前辈所说的。
能不能把我这个修改一下,我再研究一下。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-24 05:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表