鱼C论坛

 找回密码
 立即注册
查看: 2468|回复: 13

[已解决]这个稍微有点难,刚学函数,大佬求教一下代码的问题

[复制链接]
发表于 2023-8-15 13:00:54 | 显示全部楼层
这是因为commond在get_int()函数里定义, 也就是说出了函数这个变量就丢了

可以在 9 和 10 行之间添加一个:global commond,设置为全局变量

另外 “指令” 的英文是 command 而不是 commond;

还有一点,command 需要是整数,可以在 10 和 11 行之间添加 commond = int(commond)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-8-15 14:37:45 | 显示全部楼层

他们是使用 CHatGPT的,我是人工,给我最佳
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-8-15 14:53:57 | 显示全部楼层    本楼为最佳答案   
本帖最后由 歌者文明清理员 于 2023-8-15 14:57 编辑
小儿无赖 发表于 2023-8-15 14:49
大哥可以帮我把问题解决了我就给你最佳

  1. """【注册】和【登陆】的代码"""

  2. #准备工作
  3. import hashlib
  4. content = {}

  5. #定义四个函数分别用于获取用户指令(get_int())、注册(register())、登陆(login())、MD5加密(encrypt())
  6. #获取用户指令(get_int())
  7. def get_int():
  8.     global commond
  9.     commond = int(input("请输入指令:"))
  10.     print("====================")

  11. #注册(register())
  12. def register():
  13.     global name, password
  14.     name = input("请输入用户名:")
  15.     password = input("请输入密码:")
  16.     print("恭喜,注册成功~")
  17.     print("====================")

  18. #MD5加密(encrypt())
  19. def encrypt():
  20.     global password
  21.     bstr = bytes(password, "utf-8")
  22.     password = hashlib.md5(bstr).hexdigest()
  23.     content[name] = password

  24. #登陆(login())
  25. def login():
  26.     while True:
  27.         name = input("请输入用户名:")
  28.         if name not in content:
  29.             print("该用户名不存在。")
  30.         else:
  31.             break
  32.     while True:
  33.         password = input("请输入密码:")
  34.         if content[name] != hashlib.md5(password.encode()).hexdigest():
  35.             print("密码错误!")
  36.         else:
  37.             print("恭喜,登录成功~")
  38.             break
  39.     print("====================")   
  40.         
  41. #开始运行   
  42. print("欢迎来到鱼C论坛~")
  43. print("====================")
  44. print("""1. 注册
  45. 2. 登陆
  46. 3. 退出""")
  47. while True:
  48.     get_int()
  49.     if commond  == 1:
  50.         register()
  51.         encrypt()
  52.     elif commond == 2:
  53.         login()
  54.     elif commond == 3:
  55.         print("====================")
  56.         break
复制代码

(参考了https://fishc.com.cn/forum.php?m ... 464&pid=6322230)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-9-26 03:00

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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